Realized something today by accident. I am asking for a dollar amount in my program, and checking to make sure it is a valid amount above $0.
What I am not checking for is a character response. I have tried some stuff, and nothing seems to work. Can anyone look at this and offer any suggestions?
#include <stdio.h> // standard input output library
//defines tax value for calculations
#define DelMar 7.25
#define Encinitas 7.5
#define LaJolla 7.75
char cAgain; //variable for Y/N questions
char cValid; // variable for validity check
float user_input() // week 4 addition for sales amount
{ //prompt, collect, and store sale amount
float fAmount; //defines amount type as float
printf("Enter Amount of Sale $ ");
scanf ("%f", &fAmount);
return fAmount;
}
int iMenu() //defines menu variable
{ // menu which prompts, collects, and stores store number
int iSelection; //defines selection type as integer
printf("Tax Calculation for Purchase\n\n"); //changed to reflect week 4 updates
printf("1. Del Mar \n2. Encinitas \n3. La Jolla\n");
printf("\n\nPlease Select Number of Store for Which to Calculate: ");
scanf("%d",&iSelection);
return iSelection;
}
int main() //main loop
{
do // week 5 do/while loop to replace GOTO and STARTOVER
{
float fSales = 0.00; // Sales value for calculations
int iStorenum; //defines store for main loop, menu will not work without
fSales = user_input(); //new week 4 loop for sales total
while (fSales <0.0 || cValid != ' ') //new week 4 loop to validate sales amount
{
printf("SALE AMOUNT CANNOT BE LESS THAN $0.00. Re-Enter: $ ");
{
scanf ("%f %c",&fSales, cValid); // scans new input for validity
}
} // ends while loop
iStorenum = iMenu(); //displays menu created above
while (iStorenum <1 || iStorenum >3) //if store number is not 1, 2 or 3
{
printf("INVALID NUMBER ENTERED! Please enter 1,2 or 3: ");
{
scanf("%d", &iStorenum); //scans input at INVALID prompt
}
} // end while loop
if (iStorenum == 1)
//Calculates and Displays Del Mar Store, Sales, Tax rate, and Tax
printf("\nDel Mar \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, DelMar, fSales*DelMar/100, DelMar*fSales/100+fSales);
if (iStorenum == 2)
//Calculates and Displays Encinitas Store, Sales, Tax rate, and Tax
printf("\nEncinitas \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, Encinitas, fSales*Encinitas/100, Encinitas*fSales/100+fSales);
if (iStorenum == 3)
//Calculates and Displays La Jolla Store, Sales, Tax rate, and Tax
printf("\nLa Jolla \tSale $%.2f\tRate %.2f%%\tTax $%.2f%\tTotal= \t$%.2f\t\n\n",fSales, LaJolla, fSales*LaJolla/100, LaJolla*fSales/100+fSales);
printf("\n\tRun Calculator Again? (y/n) ");//prompts for repeat
scanf("%c", &cAgain);//scans input at store number prompt
} while (cAgain == 'y' || cAgain == 'Y'); // week 5 do/while end
return 0; // successful main loop end
}