Below is my code. The problem is the program does compile and run, but it is not right. When u enter a numeric value it gives u the tax for all 3 locations; however, when you enter a letter or letters it lets u loop back to re enter a correct number and it will loop back several times if u continue to enter letters instead of numbers. The problem is if after u enter letters and it loops back, if you enter a number the program still thinks its an invalid entry. So what am i doing wrong. Please exlain to me what i am doing wrong so I can fix this issue, thanks.
Code:
#include <stdio.h> // Include standard I/O libraries
#include <stdlib.h> // Include standard libraries
#include <math.h> // Include Math libraries
/* Defining tax rates for Kudler's three store */
float DelMarRate = 7.25; /* Del Mar tax rate */
float EncinitasRate = 7.5; /* Encintas tax rate */
float LaJollaRate = 7.75; /* LaJolla tax rate */
/* Variables for Kudler's three store's tax calculation */
float DelMarTax = 0.0; /* Calculation variable for Del Mar taxes */
float EncinitasTax = 0.0; /* Calculation variable for Encintas taxes */
float LaJollaTax = 0.0; /* Calculation variable for LaJolla taxes */
float Amount = 0.0;
int main(void)
{
char Purchase[32]; /* Declare variables */
int i,Return=0;
/* Prompt user for Purchase Amount */
printf("\n*********************************");
printf("\n* Welcome to Kudler Fine foods! *");
printf("\n*********************************");
printf("\n\n");
printf("\n\nPlease Enter the customer's purchase amount:$");
scanf("%s", Purchase);
do{
/* Loop - Test the numeric values*/
for(i=0; Purchase[i]; i++)
{
if(Purchase[i] < '0' || Purchase[i] > '9') /* Test for a numeric value (0-9) */
{
if (Purchase[i] == '.') /* If character is a decimal continue */
continue;
else
Return=1; /* Set Non-Numerical Value flag */
break;
}
}
if (Return == 1) /* If Non-Numerical Value print Error Message and allow user to re-enter purchase price */
{
printf("\n\nError! Invalid Purchase Amount [ %s ]\n",Purchase);
fflush(stdin);
printf("\n\nPlease Re-enter Purchase Amount:$");
scanf("%s", Purchase);
}
}while (Return == 1);
Amount=atof(Purchase); /* Convert to Float */
DelMarTax=(Amount*DelMarRate)/100; /* Calculate Tax Amount for Del Mar */
EncinitasTax=(Amount*EncinitasRate)/100; /* Calculate Tax Amount for Encinitas */
LaJollaTax=(Amount*LaJollaRate)/100; /* Calculate Tax Amount for La Jolla */
/* Display Tax Calculation output to the screen */
printf("\n\n\n");
printf("Del Mar\t\tEncinitas\tLa Jolla\t\n");
printf("*******************************************\n");
printf("$%.2f\t\t$%.2f\t\t$%.2f\n", DelMarTax, EncinitasTax, LaJollaTax);
printf("\n\nPlease Press The Enter Key To Exit Program!\n\n");
fflush(stdin);
/*Keeping DOS Screen open so user can view output*/
scanf("%c\n");
return 0;
}