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;}

A good start is something along these lines:

#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;
}

It still doesn't protect you from extraneous characters after a valid number, but how you deal with that is application specific.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.