View Single Post
Old 05-14-2004, 05:58 PM   #3 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Hi, I noticed you wish to work in the standard namespace, yet you use cin.getline().
Use getline().
Here is a nice piece of code to validate correct input. You can change the int to anyting like a bool in your case.
Code:
  //uses <strstream>
  string sInLine;
  bool bValid = false;
  bool bNonDigit = false;
  int  nNumber;
  
  while( !bValid ) {
    cout << "Enter an integer : ";
    getline( cin, sInLine );
    
    bNonDigit = false;
    for( size_t i = 0; i < sInLine.size(); ++i ) {
      if( !isdigit( sInLine[i] ) )
        bNonDigit = true;
    }
    
    if( bNonDigit )
      cout << "The input contains non digits - Invalid\n";
    
    else {
      istrstream InStream( sInLine.c_str() );
      InStream>> nNumber;
      if( !InStream )
        cout << "Your number is invalid\n";
      else
        bValid = true;
    }
  }
  
  cout << "You entered " << nNumber << endl;
__________________
Valmont is offline   Reply With Quote