As you can see, I test if there is a valid code that is : 5 digits, first digit must be between 1-6, 2nd digit is between 1-4 depending on the first digit, and 00 computes total.
I haven't finished coding the portion of what to do if a valid code is entered yet but, when I test it with an invalid code, it keeps looping "Enter Item Code:" or previously it would say "<INVALID CODE, PLEASE TRY AGAIN" and then it would say "Enter Quantity:" instead of going back to "Enter Item Code:" can anyone help?
Code:
SAMPLE OUTPUT
----------------------------------------------------------------------
_-=== Windsor Market Farm Fresh ===-_
Welcome!
Enter Item code: 12199
Enter Quantity: 2.5
> Produce Bulk, local fruits, $1.99/lb @ 2.5 = $4.98
Enter Item code: 44099
Enter Quantity: 3
> Dairy, yogurt, $0.99 each @ 3 = $2.97
Enter Item code: 4444
<INVALID CODE, PLEASE TRY AGAIN>
Enter Item code: 00
----------------------------------------------------------------------
> SUB-TOTAL $7.95
> TAX@7% $0.56
----------------------------------------------------------------------
> TOTAL $4.45
----------------------------------------------------------------------
Enter CASH AMOUNT: 20
>CHANGE TO CUSTOMER DUE: $15.55
----------------------------------------------------------------------
THANK YOU FOR SHOPPING AT WINDSOR MARKET FARM FRESH, PLEASE COME AGAIN
----------------------------------------------------------------------
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;
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;
//Check 1st digit
else if (code / 10000 == 7)
validCode = 0;
else if (code / 10000 == 8)
validCode = 0;
else if (code / 10000 == 9)
validCode = 0;
//Check 2nd digit
else if (code / 1000 % 10 > 4)
validCode = 0;
else if (code / 10000 == 6 && code / 1000 % 10 > 0)
validCode = 0;
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
printf("Enter Quantity: "); // prompt quantity
scanf("%f", &quantity); // retrieve from keyboard
}while(code !=0 || code != 00); //loop will be repeated until item code 0 is entered
//print and calculation statments 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");
printf("Enter Cash Amount: ");
scanf("%f", &paidCash);
printf("\t> CHANGE TO CUSTOMER DUE: $%.2f\n", paidCash - subTotal * 1.07);
//Display Footer
PrintFooter();
return 0;
}