Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,617 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 995 people online right now. Registration is fast and FREE... Join Now!




User Input

 
Reply to this topicStart new topic

User Input, How to Let a User Answer a Question

legault
post 3 Apr, 2006 - 08:40 PM
Post #1


New D.I.C Head

*
Joined: 14 Feb, 2006
Posts: 19


My Contributions


Hi. I'm trying to write a part of a program that will allow the user to enter an answer (yes, or no) in response to a question (do you want to play again). I'm not sure what the best way to do this is. so far I hav:

if () char *choice == "Yes") {
goto 16();}

anyone have any tips? thanx
User is offlineProfile CardPM

Go to the top of the page

sontek
post 4 Apr, 2006 - 01:43 AM
Post #2


D.I.C Regular

Group Icon
Joined: 13 Sep, 2001
Posts: 283



Thanked 1 times

Dream Kudos: 85
My Contributions


Do not use goto statements, that will produce "Spaghetti code" tongue.gif


A better way to do this would be:

CODE

cin >> response;
if(response == "Yes")
  PlayAgain();
else
  EndGame();


and then create 2 methods
CODE

void PlayAgain()
{
   SaveScore();
   Reset();
}
void EndGame()
{
  SaveScore();
  Exit();
}
User is offlineProfile CardPM

Go to the top of the page

bluesuus
post 5 Apr, 2006 - 06:30 AM
Post #3


D.I.C Head

Group Icon
Joined: 26 Dec, 2005
Posts: 57



Dream Kudos: 25
My Contributions


Yea as sontek said...but jus put everything ina while loop

cin >> respose;

while ( response == "YES" )
{
PlayAgain();

cout << "Do you wana play again\n";
cin >> response;
}

cout <<"Thanks 4 playing";
User is offlineProfile CardPM

Go to the top of the page

henry
post 5 Apr, 2006 - 11:23 PM
Post #4


D.I.C Head

**
Joined: 3 Jan, 2006
Posts: 96


My Contributions


You can also use do while,

-----------------------------------------------------------------------------------
if me is you, I will use integer. Easier. If char more tedious. You don't know what the user will input whether is "YES" , "yES", "yeS","yes"........
THink about it!
huh.gif

while ( response == 1 && response ==2)
{
PlayAgain();

cout << "Do you wana play again\n";
cout<<"Select:\n"
<<"1 - Yes\n"
<<"2 - No\n";
int response;
cin >> response;

}

cout <<"Thanks 4 playing"; tongue.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 03:12AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month