| | |
Basic Error Checking
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2005
Posts: 1
Reputation:
Solved Threads: 0
Aloha,
I am trying to add some basic error checking to my program. I have it only accepting the correct numbers (Positive) but when a 'char' is entered, it displays my message infinately.
Any ideas?
cout << "For DEBUG information press 1, to omit, press 0:";
cin >> DEBUG;
if ((DEBUG != 1) && (DEBUG != 0))
//this works for numbers but not letters
{cout <<"Sorry, that was not a valid entry, Please try again." << endl;
cout << "For DEBUG information press 1, to omit, press 0:";
cin >> DEBUG;}
I am trying to add some basic error checking to my program. I have it only accepting the correct numbers (Positive) but when a 'char' is entered, it displays my message infinately.
Any ideas?
cout << "For DEBUG information press 1, to omit, press 0:";
cin >> DEBUG;
if ((DEBUG != 1) && (DEBUG != 0))
//this works for numbers but not letters
{cout <<"Sorry, that was not a valid entry, Please try again." << endl;
cout << "For DEBUG information press 1, to omit, press 0:";
cin >> DEBUG;}
A good start is something along these lines:
It still doesn't protect you from extraneous characters after a valid number, but how you deal with that is application specific.
C++ Syntax (Toggle Plain Text)
#include <iostream> using namespace std; int main() { int n; while ( !( cin>> n ) && !cin.eof() ) { cerr<<"Invalid input"<<endl; cin.clear(); cin.ignore ( cin.rdbuf()->in_avail() ); } if ( !cin.eof() ) cout<<"You entered "<< n <<endl; }
I'm here to prove you wrong.
![]() |
Similar Threads
- Military time error checking (C++)
- Custom error checking (C#)
- error checking help (C++)
- User Form error checking (PHP)
- Help with 1.pointers and 2.error checking (C++)
- Error Checking for user input (Java)
- error checking of user input (C++)
- How to Perform Disk Error Checking in Windows XP (Windows tips 'n' tweaks)
Other Threads in the C++ Forum
- Previous Thread: Question about comparing a string with a character
- Next Thread: Game Engine
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count database delete deploy desktop developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker list loop looping loops map math memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference rpg sorting string strings struct temperature template test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






