944,028 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 4595
  • C RSS
Jan 3rd, 2007
0

Handling Invalid User Input?

Expand 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.
cpp Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
TomPettyJT is offline Offline
1 posts
since Jan 2007
Jan 3rd, 2007
0

Re: Handling Invalid User Input?

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

Click to Expand / Collapse  Quote originally posted by TomPettyJT ...
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
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Jan 3rd, 2007
0

Re: Handling Invalid User Input?

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,953 posts
since Aug 2005
Jan 3rd, 2007
0

Re: Handling Invalid User Input?

Click to Expand / Collapse  Quote originally posted by TomPettyJT ...
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.
Super Moderator
Featured Poster
Reputation Points: 3241
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,873 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Help with string
Next Thread in C Forum Timeline: marks awarding C program





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC