954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Handling Invalid User Input?

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 );
    }
}
TomPettyJT
Newbie Poster
1 post since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

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

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 
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 .

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You