View Single Post
Old May 22nd, 2006, 7:44 AM   #13
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,107
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by tumbleTetris
ooh, another little technicality.

if a user inputs something that is not 1 2 or x, how can I tell them they made a mistake and should reinput something 1 2 or x.
You use data validation. When you get the data from the user, you are expecting it to match certain criteria. Check that it matches, and if it does not, you query the user again.

You can use repeated conditional statements to test for validity, or (in certain cases, such as yours) build up a list of valid inputs (in an array, for example), and then iterate through the list, checking each time.
Quote:
Originally Posted by tumbleTetris
If I use else if's I can't ask again, can I?
Sure you can. Probably the easiest way is to set a 'validity' flag when you test the input. For example, say you have a switch() block, or series of if() statements. Set the flag to false before you get the input. Then you test it in each case or if() statement, and if it is valid, you set the flag to true. However, only set it to false at the beginning of the testing process (otherwise, it might be set to true when you test for valid input A, and then to false when it's not valid input B, resulting in your program rejecting perfectly valid input). You then use this flag as the test condition in a loop:
do
{
  valid = false;
  // prompt user for input here
  // initialize 'userInput' variable with input from user
  if(userInput == whateverItShouldBe)
	valid = true;
} while(!valid);
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote