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 );
    }
}

First mistake: you didn't use code tags. There's more information in my signature.

void main()
{

Don't use void main. More info in my signature. :cheesy:

To make sure that the user entered what you were expecting, always check the return value of scanf, as it returns the number of read items from the input buffer. If it's 0 or less than 0, you need to ask the user to reenter the data.

Hope this helps

I would suggest getting input as a string so that you can detect invalid characters. scanf() won't necessarily do that for you. For example you might type "123.45abcd" and scanf() will accept everything up to the first non-numeric character -- 'a' -- without any complaints. But if you want to produce a warning or error to the user then you should get the input as a string (such as call fgets() ) and validate each character.

Comments
Ah, I forgot about that. :) -joeprogrammer

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.

For the purpose you are trying to achieve, I wouldn't recommend scanf. It offers so very little control over what the user enters and also validation becomes a pain. Better accept the entire input as string and write your own validation functions. More info here.

This article has been dead for over six months. Start a new discussion instead.