| | |
Basic Error Checking
![]() |
•
•
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 binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph guess gui homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






