Thread: User input
View Single Post
Old 04-17-2005, 01:48 PM   #1 (permalink)
teknomage1
Jack of all trades
 
teknomage1's Avatar
 
Join Date: Feb 2005
Location: Los Angeles
Posts: 598
teknomage1 is on a distinguished road
Send a message via AIM to teknomage1
User input

So I'm taking a C++ class right now and we haven't gotten to strings yet but we've had to write programs that take user input, often as integers. But if the user puts in something that's not an int the program goes into an infinite loop. What's the proper way to read input?
Code:
    int numRows;
    do {
          cout << "Enter an odd number between 1 and 19 : ";
          cin >> numRows;
     } while (numRows < 0 || numRows > 20 || !(numRows % 2) );
teknomage1 is offline   Reply With Quote