| | |
Newbie Question: Error Checking User Input
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jun 2008
Posts: 3
Reputation:
Solved Threads: 0
I'm a newbie to c++, and as I'm reading my textbook, I'm applying different rules to this program for converting gallons to liters and vice versa, My question is how can I check that the user input is numeric and not a string of characters? I've tried several things with isalpha and isdigit, but they didn't seem to work and the program just continued to return an infinite loop. I really appreciate any input on this.
Thanks!
Thanks!
c++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { const double GAL_CONVERSION = 3.78533; const double LIT_CONVERSION = .264; double gallons, litres; char ans; const char error_neg[] = "Unable to calculate negative volume, please enter a positive amount\n"; cout << "\nWelcome to the Volume Converter!\n" << "Enter 0 to quit" <<endl; do { cout <<"Please insert the number of gallons you would like to convert: "; cin >> gallons; litres = gallons * GAL_CONVERSION; if (gallons > 0) { cout << "You have " << litres << " litres " << "if you have " << gallons; if (gallons == 1) cout << " gallon\n"; else cout << " gallons\n"; } if (gallons < 0) cerr << error_neg; else; } while (gallons != 0); cout << "Do you want to convert litres to gallons? (y/n): "; cin >> ans; if (ans == 'y') { do { cout << "please enter the number of liters you would like to convert: "; cin >> litres; gallons = litres * LIT_CONVERSION; if (litres > 0) { cout << "You have " << gallons << " gallons " << "if you have " << litres; if (litres == 1) cout << " litre\n"; else cout << " litres\n"; } if (litres < 0) cerr << error_neg; }while (litres != 0); } else; cout << "Good-bye\a" << endl; return 0; }
Last edited by Koinutron; Aug 4th, 2009 at 4:31 pm. Reason: missed closing bracket in code paste
Get the input as a string then check each of the characters. If all is ok then convert that string to int (or other data type). Something along these lines.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <string> #include <sstream> using namespace std; int main() { int gallons; std::string input; cout << "Enter gallons\"; cin >> input; bool ok = true; for(int i = 0; i < input.length(); i++) { if( !isdigit(input[i]) ) { cout << "Error\n"; ok = false; } } if( ok == true ) { stringstream str(input); str >> gallons; }
Last edited by Ancient Dragon; Aug 4th, 2009 at 4:36 pm.
if you don't want to get the user's input as a string you can
do the following (not tested) :
do the following (not tested) :
C++ Syntax (Toggle Plain Text)
int num = 0; cout<<"Number please : "; cin >> num; while(!cin)//if input fails { cin.clear(); while(cin.get() != '\n') continue; cout<<"\nInvalid Input\n"; cout<<"Try again : "; cin >> num; }
![]() |
Similar Threads
- Help with 1.pointers and 2.error checking (C++)
- Error Checking for user input (Java)
- error checking of user input (C++)
- Need Help With Error Checking User Input (C)
Other Threads in the C++ Forum
- Previous Thread: srand() issue
- Next Thread: Link ws2_32.lib
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data database delete desktop developer directshow dll download dynamic encryption error file forms fstream function functions game generator getline givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project proxy python random read recursion recursive return string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






