- #1
Saterial
- 54
- 0
Hello, I am working on a basic cash register c program and I've run into a few errors/logical errors I don't know where to begin to fix.
I am limited to knowing only how to use, scant, printf, if/elseif/else, do/dowhile. The problem is if you compile it and test it, my validcode flag is not working. It seems that the entered product code is always seen as valid. For example if I enter either 9999 or 999999, the program still sees it as valid and prompts for quantity.
Why is this? I have
So why is it seeing it as valid all the time? If I enter an invalid code it keeps asking for quantity and entering item code in a loop all over again.
How can I go about fixing this?
I am limited to knowing only how to use, scant, printf, if/elseif/else, do/dowhile. The problem is if you compile it and test it, my validcode flag is not working. It seems that the entered product code is always seen as valid. For example if I enter either 9999 or 999999, the program still sees it as valid and prompts for quantity.
Why is this? I have
Code:
//Check if 5 digits
if (code < 10000 && code > 99999){
validCode = 0;
}
else
{
validCode = 1;
}
So why is it seeing it as valid all the time? If I enter an invalid code it keeps asking for quantity and entering item code in a loop all over again.
How can I go about fixing this?
Code:
#include <stdio.h>
//function prototypes
void PrintHeader();
void PrintFooter();
void PrintHeader()
{
printf("---------------------------------------------------------------------\n\n");
printf(" _-=== Windsor Market Farm Fresh ===-_\n\n");
printf("Welcome!\n");
}
void PrintFooter()
{
printf("---------------------------------------------------------------------\n\n");
printf("THANK YOU FOR SHOPPING AT WINDSOR MARKET FARM FRESH, PLEASE COME AGAIN\n\n");
printf("---------------------------------------------------------------------\n");
}
int main()
{
int code; //use this variable to enter the product codes
float quantity;
float subTotal = 0;
float paidCash;
float price;
int validCode;
//Display Heading
PrintHeader();
//Main Program loop
do
{
do
{
printf("Enter Item Code: "); //prompt for item code
scanf("%d", &code); //retrieve input from keyboard
//Check if 5 digits
if (code < 10000 && code > 99999){
validCode = 0;
}
else
{
validCode = 1;
}
//Check 1st digit
if (code / 10000 == 7)
{
validCode = 0;
}
else
{
validCode = 1;
}
if (code / 10000 == 8)
{
validCode = 0;
}
else
{
validCode = 1;
}
if (code / 10000 == 9)
{
validCode = 0;
}
else
{
validCode = 1;
}
//Check 2nd digit
if (code / 1000 % 10 > 4)
{
validCode = 0;
}
else
{
validCode = 1;
}
if (code / 10000 == 6 && code / 1000 % 10 > 0)
{
validCode = 0;
}
else
{
validCode = 1;
}
if (validCode == 0){ //if code is invalid, it will loop back to the beginning and ask for item code again
printf("<INVALID CODE, PLEASE TRY AGAIN>\n");
}
}while (validCode != 1); // if code is valid, prompt for quantity
if ((validCode == 1 && code != 0) || code != 00){
printf("Enter Quantity: "); // prompt quantity
scanf("%f", &quantity); // retrieve from keyboard
}
if (code / 10000 == 1 && code / 1000 % 10 == 1){
price = (float)(code % 1000) / 100;
printf("\t > Produce Bulk, local vegetables, $%.2f/lb @ %.1f = %15.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 1 && code / 1000 % 10 == 2){
price = (float)(code % 1000) / 100;
printf("\t > Produce Bulk, local fruits, $%.2f/lb @ %.1f = %19.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 1 && code / 1000 % 10 == 3){
price = (float)(code % 1000) / 100;
printf("\t > Produce Bulk, imports, $%.2f/lb @ %.1f = %24.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 2 && code / 1000 % 10 == 1){
price = (float)(code % 1000) / 100;
printf("\t > Produce Bulk, local vegetables, $%.2f each @ %.1f = %13.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 2 && code / 1000 % 10 == 2){
price = (float)(code % 1000) / 100;
printf("\t > Produce Bulk, local fruits, $%.2f each @ %.1f = %17.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 2 && code / 1000 % 10 == 3){
price = (float)(code % 1000) / 100;
printf("\t > Produce Bulk, imports, $%.2f each @ %.1f = %22.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 3 && code / 1000 % 10 == 1){
price = (float)(code % 1000) / 100;
printf("\t > Floral, roses, $%.2f each @ %.1f = %30.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 3 && code / 1000 % 10 == 2){
price = (float)(code % 1000) / 100;
printf("\t > Floral, bouquet, $%.2f each @ %.1f = %28.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 3 && code / 1000 % 10 == 3){
price = (float)(code % 1000) / 100;
printf("\t > Floral, house plant, $%.2f each @ %.1f = %24.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 4 && code / 1000 % 10 == 1){
price = (float)(code % 1000) / 100;
printf("\t > Dairy, milk, $%.2f each @ %.1f = %32.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 4 && code / 1000 % 10 == 2){
price = (float)(code % 1000) / 100;
printf("\t > Dairy, eggs, $%.2f each @ %.1f = %32.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 4 && code / 1000 % 10 == 3){
price = (float)(code % 1000) / 100;
printf("\t > Dairy, butter, $%.2f each @ %.1f = %30.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 4 && code / 1000 % 10 == 4){
price = (float)(code % 1000) / 100;
printf("\t > Dairy, yogurt, $%.2f each @ %.1f = %30.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 5 && code / 1000 % 10 == 1){
price = (float)(code % 1000) / 100;
printf("\t > Meats, beef, $%.2f/lb @ %.1f = %34.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 5 && code / 1000 % 10 == 2){
price = (float)(code % 1000) / 100;
printf("\t > Meats, chicken, $%.2f/lb @ %.1f = %31.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 5 && code / 1000 % 10 == 3){
price = (float)(code % 1000) / 100;
printf("\t > Meats, turkey, $%.2f/lb @ %.1f = %32.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 5 && code / 1000 % 10 == 4){
price = (float)(code % 1000) / 100;
printf("\t > Meats, pork, $%.2f/lb @ %.1f = %34.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 5 && code / 1000 % 10 == 5){
price = (float)(code % 1000) / 100;
printf("\t > Meats, rabbit, $%.2f/lb @ %.1f = %32.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
else if (code / 10000 == 6 && code / 1000 % 10 == 0){
price = (float)(code % 1000) / 100;
printf("\t > Other, other item, $%.2f each @ %.1f = %26.2f\n", price, quantity, price * quantity );
subTotal = subTotal + price * quantity;
}
}while(code !=0); //loop will be repeated until item code 0 is entered
//print and calculation statements to determine subtotal, total, tax and change
printf("---------------------------------------------------------------------\n");
printf("\t> SUB-TOTAL\t %.2f\n", subTotal);
printf("\t> TAX@7%%\t %.2f\n", subTotal * 0.07);
printf("---------------------------------------------------------------------\n");
printf("\t> TOTAL\t %.2f\n", subTotal * 1.07);
printf("---------------------------------------------------------------------\n\n");
printf("Enter Cash Amount: ");
scanf("%f", &paidCash);
printf("\t> CHANGE TO CUSTOMER DUE: $%.2f\n\n", paidCash - subTotal * 1.07);
//Display Footer
PrintFooter();
return 0;
}