| | |
Handling Invalid User Input?
![]() |
•
•
Join Date: Jan 2007
Posts: 1
Reputation:
Solved Threads: 0
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)
#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 ); } }
Last edited by WaltP; Jan 3rd, 2007 at 9:51 pm. Reason: Please use code tags -- see the BBCODE announcement
First mistake: you didn't use code tags. There's more information in my signature.
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
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."
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.
•
•
•
•
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.
I don't accept change; I don't deserve to live.
![]() |
Similar Threads
- user input into a string (C++)
- error checking of user input (C++)
- Creating a GUI that accepts user input help (Java)
- Need Help With Error Checking User Input (C)
- filtering bad user input (Java)
- Mortgage Program help (Java)
Other Threads in the C Forum
- Previous Thread: Help with string
- Next Thread: marks awarding C program
| Thread Tools | Search this Thread |
* ansi api array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copypdffile createcopyoffile createprocess() csyntax directory dynamic execv fflush file floatingpointvalidation fork forloop frequency function getlasterror getlogicaldrivestrin givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hardware highest histogram homework i/o ide inches intmain() iso km license linked linkedlist linux linuxsegmentationfault list logical_drives looping loopinsideloop. lowest match matrix microsoft motherboard mqqueue mysql oddnumber odf open opendocumentformat openwebfoundation pdf pointer posix power program programming pyramidusingturboccodes read recursion recv recvblocked repetition reversing scanf scheduling segmentationfault send shape single socketprogramming stack standard strchr string suggestions test unix urboc user variable whythiscodecausesegmentationfault win32api windows.h windowsapi






