I have been struggling with this program assignment for weeks now. Can someone offer some advice on how to only accept numerical input, and to reject anything else? Here is my code.
#include <stdio.h>
#include <ctype.h>
void main()
{
//Declaration of Variables
float kPrice; //Price to Calculate
int choice;
int runagain = 1; //Choice of Store, //For while loop
printf( "\nKundler Fine Foods Tax Calculator \n by John R. Trevisan\n" ); //Title
while ( runagain == 1 )
{ //Start loop
//Tax calculations and Total calculations by location.
printf( "\nPlease enter the price you would like me to display the tax and total sale on?\n" );
scanf( "%f", & kPrice );
printf( "\n\t\t\t You entered $%.2f\n", kPrice );
printf( "\nWhich of our locations will you shop at? I can show you the total tax and total price on your purchase.\n" );
printf( "\n1. The Del Mar store 2. the Encitas store 3. the LaJolla store? \n" );
scanf( "%d", & choice ); //Input of store choice
printf("You entered %d\n", choice);
printf("Please enter a digit");
switch ( choice ){
case 1: //Del Mar
printf( "\nThe sales tax at the Del Mar store is only: $%.2f\n", ( kPrice *.0725 ) ),
printf( " therefore, your total sale at the Del Mar store would be = $%.2f\n", ( kPrice *.0725 + kPrice ) );
break;
case 2://Encitas
printf( "\nThe sales tax at the Encitas store is only: $%.2f\n", ( kPrice *.0750 ) ),
printf( " therefore, your total sale at the Encitas store would be = $%.2f\n", ( kPrice *.0750 + kPrice ) );
break;
case 3://La Jolla
printf( "\nThe sales tax at the LaJolla store is only: $%.2f\n", ( kPrice *.0775 ) ),
printf( " therefore, your total sale at the LaJolla store would be = $%.2f\n", ( kPrice *.0775 + kPrice ) );
break;
default:
printf( "\nTsk, Tsk, Tsk. BAD CHOICE! That's not what I asked!\n" );
} //End Switch
printf( "\nPlease select the number 1 to start again, or any number key to exit. \n If you press a letter you'll enter an INFINITE LOOP!!!\n" ); //ethical
// choice :)
scanf( "%d", & runagain );
}
}