Archived

This topic is now archived and is closed to further replies.

node_5r

PROGRAMMING CRISIS!!!!

Recommended Posts

hey all! I have a bit of a problem. I am new to programming, well not that new but i cant figure out how to do and if and else if in c++. I am going to make a rpg text based game. In the beginning of it i want to ask if the player wishes to continue or not. If the user presses y then he/she will continue. If the user presses n then it will exit the game. So can anyone help by showing me how to do this?? thanx node_5r

Share this post


Link to post
Share on other sites
I''m wondering, might you mean something like

char cont;
cin >> cont;
if (cont == ''n'')
exit(0);

''here you write the code for the yes answer''

This code isn''t even near fail safe, but it''ll do the job.


A polar bear is a rectangular bear after a coordinate transform.

Share this post


Link to post
Share on other sites
Easy stuff.


void main() {
char entry;
cout << "Do you wish to continue? (y/n)\n?";
cin >> entry;
if(entry == "n") {
cout << "\nThank you for playing!\n;
}
else if(entry == "y") {
DoGame();
}
}
void DoGame()
{ ... }


there ya have it. a simple game main function. A better way would be as follows:


void main() {
bool done = false;
while(!done) {
if(!DoGame()) done = false;
}
}
bool DoGame() {
char entry;
cout << "Do you wish to continue? (y/n)\n?";
cin >> entry;
if(entry == "n") {
return false;
}
.
.
.
return true;
}


*oof*

Share this post


Link to post
Share on other sites
I''ve found to use if and else you HAVE to have brackets like this:
if (value == ''y''){
//Put code here
} else {
//put more code here
}

"When people tell you they want to hear the truth, you know that their lying."

Share this post


Link to post
Share on other sites
you don''t have to use braces for every else statement, ya fool!
this if statement, for example, doesn''t use the braces(doesn''t have anything to do with initial post because they indeed do need braces):

if (userGuess > Number)
cout << "Too big!\n";
else
if (userGuess < Number)
cout << "Too small!\n";
else // you need brackets here, because it is actually doing something!
{
cout << "You guessed the same as Number!";
return (0);
} // end else
} // end main or whatever

as you can see, not all if and else statements need braces. You''re very very wrong about that...




Programming::~Fredric(const Annoy_Ance)

Share this post


Link to post
Share on other sites
To clarify the braces thing, here''s the syntax for if...else:

if (condition) statement;
else statement2;

Braces indicate the beginning and end of a compound statement, so if either "statement" or "statement2" in the above syntax is a compound statement then it needs braces. Thus:

if (something == TRUE)
DoThis();
else
{
DoThat();
DoSomethingElse();
DoOneMoreThing();
}

That''s pretty much what Fredric was saying, but I wanted to clarify specifically why you would or would not need braces.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
If when you ask whether they want to continue or not, do you mean:
1)You don''t mind if they type other characters, and you want them to press enter to confirm their choice. If true, other people have already answered it.
2)You want immediate action after they press a letter without waiting for them to press enter. Then it depends on what operating system you are using. If windows, you can always use virtual keys, if dos or other OS''s, you might have to use assembler.

Share this post


Link to post
Share on other sites
here goes:

char response;

if(response = getch() == 0)
response = getch();
if(response == 'y' // response == 'Y')
cout << "Y pressed.\n";
else {
cerr << "User break in program.";
return 1;
}

To use cin, cout, cerr, #include
To use getch(), #include
Note that getch() doesn't work in windows applications.
Also that any key other than y will exit.

Edited by - abe_bcs on 3/29/00 8:39:45 PM

Share this post


Link to post
Share on other sites
About those braces... it is an EXTREMELY good idea to put them in whenever you are using and "else if" statement since programmers normaly interpert this differently than the compiler does. For example:


if (foo)
doSomething();
else if (bar)
doSomethingElse();
else
doTheOtherThing();


will be interpreted as:


if (foo)
{
doSomething();
}
else
{
if (bar)
{
doSomethingElse();
}
else
{
doTheOtherThing();
}
}


See how the statements are really nested? Well, most programmers think or else if more as a switch statement where all the clauses are on the same level, but this is actually not the case. It is fine to think of it this way unless not all your if''s have elses... In this case you can run into troubles where you think that an else is matched with one if and the compiler thinks it is matched with another (I had this problem in some error handling code a while back)

So, basically, using the brackets is always a good idea b/c then you are certain that you and the compiler will agree on the proper nesting and matching orders!


Check out the GPI project today!

Share this post


Link to post
Share on other sites