Handling Invalid User Input?

Reply

Join Date: Jan 2007
Posts: 1
Reputation: TomPettyJT is an unknown quantity at this point 
Solved Threads: 0
TomPettyJT TomPettyJT is offline Offline
Newbie Poster

Handling Invalid User Input?

 
0
  #1
Jan 3rd, 2007
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.
  1. #include <stdio.h>
  2. #include <ctype.h>
  3. void main()
  4. {
  5. //Declaration of Variables
  6. float kPrice; //Price to Calculate
  7. int choice;
  8. int runagain = 1; //Choice of Store, //For while loop
  9. printf( "\nKundler Fine Foods Tax Calculator \n by John R. Trevisan\n" ); //Title
  10. while ( runagain == 1 )
  11. { //Start loop
  12. //Tax calculations and Total calculations by location.
  13. printf( "\nPlease enter the price you would like me to display the tax and total sale on?\n" );
  14. scanf( "%f", & kPrice );
  15. printf( "\n\t\t\t You entered $%.2f\n", kPrice );
  16. printf( "\nWhich of our locations will you shop at? I can show you the total tax and total price on your purchase.\n" );
  17. printf( "\n1. The Del Mar store 2. the Encitas store 3. the LaJolla store? \n" );
  18. scanf( "%d", & choice ); //Input of store choice
  19. printf("You entered %d\n", choice);
  20. printf("Please enter a digit");
  21. switch ( choice ){
  22. case 1: //Del Mar
  23. printf( "\nThe sales tax at the Del Mar store is only: $%.2f\n", ( kPrice *.0725 ) ),
  24. printf( " therefore, your total sale at the Del Mar store would be = $%.2f\n", ( kPrice *.0725 + kPrice ) );
  25. break;
  26. case 2://Encitas
  27. printf( "\nThe sales tax at the Encitas store is only: $%.2f\n", ( kPrice *.0750 ) ),
  28. printf( " therefore, your total sale at the Encitas store would be = $%.2f\n", ( kPrice *.0750 + kPrice ) );
  29. break;
  30. case 3://La Jolla
  31. printf( "\nThe sales tax at the LaJolla store is only: $%.2f\n", ( kPrice *.0775 ) ),
  32. printf( " therefore, your total sale at the LaJolla store would be = $%.2f\n", ( kPrice *.0775 + kPrice ) );
  33. break;
  34. default:
  35. printf( "\nTsk, Tsk, Tsk. BAD CHOICE! That's not what I asked!\n" );
  36. } //End Switch
  37. 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
  38. // choice :)
  39. scanf( "%d", & runagain );
  40. }
  41. }
Last edited by WaltP; Jan 3rd, 2007 at 9:51 pm. Reason: Please use code tags -- see the BBCODE announcement
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Handling Invalid User Input?

 
0
  #2
Jan 3rd, 2007
First mistake: you didn't use code tags. There's more information in my signature.

Originally Posted by TomPettyJT View Post
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
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,398
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Handling Invalid User Input?

 
0
  #3
Jan 3rd, 2007
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,615
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 466
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Handling Invalid User Input?

 
0
  #4
Jan 3rd, 2007
Originally Posted by TomPettyJT View Post
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.
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC