View Single Post
Old 10-17-2004, 12:58 PM   #22 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
To solve the empty-string-puzzle just change menu into this:
Code:
void menu()
{
   string errmsg("ERROR! Reason: could not find valid input.\n");
   for( unsigned i = 0; i < RACERS; ++i )
   {
      cout << "Enter the name of racer #" << i+1 << ": ";
      while( !(getline( cin, theRacers[i].sName)) || theRacers[i].sName.size() == 0 )
      {
         cout<<errmsg;
         cout<<"RETRY (did you enter a name?): ";
         reset_istream();
      }
      cout << "Enter the finish time (minutes) of racer #" << i+1 << ": ";
      while( !(cin >> theRacers[i].nMinutes) )
      {
         cout<<errmsg;
         cout<<"RETRY (finish time in minutes): ";
         reset_istream();
      }
      reset_istream();
   }
}
This code also intercepts input of CTRL-Z/D. That's why the reset_istream() function looks so weird by the way. In this case no terminating character clearance is desired. Otherwise when you try to input a new value, the first terminating character is going to be eaten away.
__________________
Valmont is offline   Reply With Quote