As always I am new to programming so please help!
In my code there are two loops used to validate y/n choices. if you run the program it work fine but error checking does not.
Here is the issue : (I have noted the lines in the attached code, If you compile and run you will see what happens)
When I enter anything but Y||N, let's say the letter P and press [enter], it counts the P and the Return and errors twice. If I enter 12345 it error 6 different times. How can I get it to count it all as one entry?
The same loop is used later at the end and if you actually select Y and press enter in the first loop, it carries the [enter] to the loop at the end of the program and errors one time.
It seems that it is counting each alpha/numeric and the return as diffrent entries. Is there some way to have it count as one entry no matter how many alpha/numeric are entered.
I marked the issue areas with //ISSUE
i also attached the *.c file if that is eaiser.
Any help would be great!!
Thx - Eric
CODE
#include <stdio.h>
#include <ctype.h>
main(void)
{
int valid_input = 0;
char cYesNo='\0';
char cRestart='\0';
float x = 1.0725f;
float y = 1.075f;
float z = 1.0775f;
float fResponse;
fResponse= 0.00f;
printf("\n*** Kudler Fine Foods - Sales Tax Calculator ***\n"); //Display program title
start:
printf("\nEnter purchase amount: "); //Prompt user to enter purchase amount//
scanf("%f", &fResponse);
if (fResponse <= 0.00){
printf("\nYou did not enter a number or your entry is less then 0\n");
goto start;
}
confirm:
printf("\nAre you sure you want sales tax calculated on: $%.2f\n"); //Verify user input//
valid_input = 0; //ISSUE -> Loop #1 error happens here
while( valid_input == 0 ) {
printf("\nContinue (Y/N)?");
scanf("%c", &cYesNo);
cYesNo = toupper(cYesNo);
if((cYesNo == 'Y') || (cYesNo == 'N') ) valid_input = 1;
else printf("\nError: Invalid choice\n");
}
switch (cYesNo){
case 'y': case 'Y':
printf("\n\nSales Tax and Total by store:");
printf("\n\n\n\tDel Mar Sales Tax=$%.2f\n", (fResponse*x)-fResponse);
printf("\n\tDel Mar Total=$%.2f\n", (fResponse*x));
printf("\n\n\tEncinitas Sales Tax=$%.2f\n", (fResponse*y)-fResponse);
printf("\n\tEncinitas=$%.2f\n", (fResponse*y));
printf("\n\n\tLa Jolla Sales Tax=$%.2f\n", (fResponse*z)-fResponse);
printf("\n\tLa Jolla=$%.2f\n", (fResponse*z));
break;
case 'n': case 'N':
goto start;
} //end switch
restart:
printf("\nWould you like to enter another amount?\n");
valid_input = 0; //ISSUE -> Loop #2 error happens here [return] from loop #1 carries to here
while( valid_input == 0 ) {
printf("\nContinue (Y/N)?");
scanf("%c", &cRestart);
cRestart = toupper(cRestart);
if((cRestart == 'Y') || (cRestart == 'N') ) valid_input = 1;
else printf("\nError: Invalid choice\n");
}
if (cRestart=='y'||cRestart=='Y'){
goto start;
}
if (cRestart=='n'||cRestart=='N'){
printf("\nThank You\n");
}
getch(); //Hold window to view results//
}//end main