What would be the best way to stop this from looping ? when an Invalid character such as a letter is input for the sale amount ?
Code:#include <stdio.h> float fAmount; main() { int iSelection = 0; float fAmount = 0.0;//Purchased amount in US$ float fTax1 = .0725;//Del Mar CA. float fTax2 = .0750;//Encinitas CA. float fTax3 = .0775;//La Jolla CA. printf("\n\tSelect the store location\n"); printf("1\tDel Mar\n"); printf("2\tEncinitas\n"); printf("3\tLa Jolla\n"); scanf("%d", &iSelection); // This makes the program run in a loop if an incorrect value is input for Purchased amount. do { printf("\nEnter Amount in US $\n"); scanf("%f", &fAmount); }while((fAmount - floor(fAmount)) == 0.0); //This calculates the tax rate for Del Mar CA. if (iSelection == 1) { printf("\nYour tax amount is: $%.2f and the total price is $%.2f\n", (fAmount * fTax1), (fAmount + (fAmount * fTax1))); printf("\nClick the X in the upper right corner to exit this application.\n"); } //end if //This calculates the tax rate for Encinitas CA. if (iSelection == 2) { printf("\nYour tax amount is: $%.2f and the total price is $%.2f\n", (fAmount * fTax1), (fAmount + (fAmount * fTax1))); printf("\nClick the X in the upper right corner to exit this application.\n"); } //end if //This calculates the tax rate for La Jolla CA. if (iSelection == 3) { printf("\nYour tax amount is: $%.2f and the total price is $%.2f\n", (fAmount * fTax1), (fAmount + (fAmount * fTax1))); printf("\nClick the X in the upper right corner to exit this application.\n"); } //end if getch(); return 0; }



LinkBack URL
About LinkBacks


