I am trying to get my program to check the validity of a entry. When the user enteres a character rather than a float, I want the program to ask them to check their entry and enter it again. I have tried the getchar(), and isdigit commands, but can't figure it out. I am completely new to this. Right now when the user enters a character rather than a float, it throws the program into a continuous loop.
any ideas???Code:#include <system.h> #include <stdio.h> #include <ctype.h> main() { // Variable declarations and initializations int iResponse = 0; float fDel_Mar_Tax = 7.25; float fEncinitas_Tax = 7.5; float fLa_Jolla_Tax = 7.75; float fPurchase_Amount = 0.0; // Program Header printf("\n\t\t Tax Calculator 2 by Aleksandr Babich\n\n\n"); // Enter purchase amount and verify purchase amount do { // do-while loop start printf("\n\nEnter the total purchase amount: $"); scanf("%f", &fPurchase_Amount); if (fPurchase_Amount <= 0.0) printf("\nInvalid entry, try again"); printf("invalid"); } //end do-while loop while (fPurchase_Amount <= 0.0); printf("\n\nTotal purchase is $ %5.2f", fPurchase_Amount); do { //start of do-while loop // Store selection printf("\n\n1\tDel Mar\n"); printf("2\tEncinitas\n"); printf("3\tLa Jolla\n"); printf("\nPlease select your shopping location (1-3): "); scanf("%d", &iResponse); // User Store selection validity if (iResponse <= 0 || iResponse >= 4) printf("\n\nInvalid entry - Please try again\n\n"); else printf("\n\n\t\t\t\t TOTALS"); switch (iResponse){ case 1: printf("\n ------------------------------------------------------------------------------\n"); printf("\n\t\tTotal purchase amoune is $%5.2f\n",fPurchase_Amount); printf("\n\t\tThe total tax on your purchaes is $%5.2f\n", fDel_Mar_Tax/100.0 * fPurchase_Amount); printf("\n\t\tYour total amount due is $%5.2f\n", fPurchase_Amount + (fDel_Mar_Tax/100.0 * fPurchase_Amount)); printf("\n\t Thank you for shopping at Kuddler Fine Foods Del Mar"); printf("\n ------------------------------------------------------------------------------\n"); break; case 2: printf("\n ------------------------------------------------------------------------------\n"); printf("\n\t\tTotal purchase amoune is $%5.2f\n",fPurchase_Amount); printf("\n\t\tThe total tax on your purchase is $%5.2f\n", fEncinitas_Tax/100.0 * fPurchase_Amount); printf("\n\t\tYour total amount due is $%5.2f\n", fPurchase_Amount + (fEncinitas_Tax/100.0 * fPurchase_Amount)); printf("\n\t Thank you for shopping at Kuddler Fine Foods Encinitas"); printf("\n ------------------------------------------------------------------------------\n"); break; case 3: printf("\n ------------------------------------------------------------------------------\n"); printf("\n\t\tTotal purchase amoune is $%5.2f\n",fPurchase_Amount); printf("\n\t\tThe total tax on your purchase is $%5.2f\n", fLa_Jolla_Tax/100.0 * fPurchase_Amount); printf("\n\t\tYour total amount due is $%5.2f\n", fPurchase_Amount + (fLa_Jolla_Tax/100.0 * fPurchase_Amount)); printf("\n\t Thank you for shopping at Kuddler Fine Foods La Jolla"); printf("\n ------------------------------------------------------------------------------\n\n\n\n"); } //end switch } while (iResponse <= 0 || iResponse >= 4); //Initiate a stop and wait for user input to continue printf("\nPress any key to continue...\n"); getch(); }//end main



LinkBack URL
About LinkBacks



