Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 21st, 2006, 9:45 AM   #11
Wizard1988
Professional Programmer
 
Wizard1988's Avatar
 
Join Date: Oct 2005
Location: Chitown
Posts: 417
Rep Power: 3 Wizard1988 is on a distinguished road
Send a message via AIM to Wizard1988
You can write a method which shows the options and receives the input. Have all your if statements for the options and then just an else for anything else. Next you can just return a boolean. If everything went ok then return true and if they type in somethign else return false.
Wizard1988 is offline   Reply With Quote
Old May 21st, 2006, 10:04 AM   #12
tumbleTetris
Programmer
 
Join Date: May 2006
Posts: 39
Rep Power: 0 tumbleTetris is on a distinguished road
yeah, thanks...

I was thinking of using arrays instead, because how I'm doing it currently means it will take a very long time.

I have to ask if its oging to be afternoon or evening, which breaks it into two groups, then I have to ask which seat they want, from a choice of 3, then finally I have to ask if they wan't concession tickets.

If I do it with arrays, I think I can forgo all the if's and elses, and in the end process it all in one go...

though I don't know how I can control the input if I use arrays, for instance if the options are 1, 2 or x, I don't know how I can tell the user of the error and give them a second chance of doing it...

If I'm headed down the wrong path with arrays, please tell me...
tumbleTetris is offline   Reply With Quote
Old May 22nd, 2006, 6:44 AM   #13
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,005
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
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 4:08 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC