   (c++) 2 noob questions ! :)
  2 questions as the title says lets get onto it. Im new to c++ so help please.1) When i use my code:#include <iostream>using namespace std;int x=100;int y=10;int main(){char answer;char answer1;cout << "Name:";cout << "Jack" << endl;    cout << "Health:";    cout << x << endl;cout << ""<< endl;cout << ""<< endl;cout << ""<< endl;cout << "Fight? Press 1" << endl;cout << "Sleep? Press 2" << endl;cin >> answer;if (answer == '1'){cout << "Get ready to fight a RABBIT !!!" << endl;cout << "Ready ??? Press 1" << endl;cout << "Return ??? Press 2" << endl;}if (answer == '2'){cout << "ZZZZZZZzzzzzzzzzzz ZZZZZZZZZZZzzzzzz" << endl;}if (answer1 == '1'){cout << "Rabbit has:" << endl;cout << y ;cout << "health" << endl;}  }And i choose say 1, it goes to this but it then closes instantly can somebody show me how i can do it so it doesnt shut down, until after the game has been completed.2) When the player chooses say option 1 there will be a option to return can you undo/return or do you have to enter the code again ??   
  First change "char answer;char answer1;"to int answer,answer1;the take the user input like this :if(answer == 1) {cout<<"answer1"<<endl;}; elseif(answer == 2) { cout<<"answer2"<<endl;};and so on. what do you mean by your question#2? Can you be a little more clearer?   ;  To answer your first question, you have a few options there. The most common is to put a cin statement before the return statement to pause the program, otherwise there is nothing else telling it to wait. i.e.:int main(){std::cout << "Hello, world!";char pause;std::cin >> pause;return 0;}On a Windows OS there is a header file that allows you to use the statement system("pause"); I'm pretty sure if you #include <system.h> that should do it but I may be wrong - it's been about 5 years since I last worked with C++.Would it be possible to rephrase your second question? I can't quite get what you're saying, I have an idea but I'd like to make sure.   ;  #1, its exits right away because its done, it did what you told it to do then it closes. However if you execute the console application through cmd it will not close right away. But if you don't want to execute it through cmd, then simply tell your program to wait at the end. You could ask for some input or use getch();You will see alot of tutorials use system("PAUSE"); but I would advise agaist using that, its not neccesary. Just ask for input and you'll be good to go.   ;  As for your second question, use some kind of looping construct. You might also find that using functions will help too.   ;Quote:Original post by xiva2) When the player chooses say option 1 there will be a option to return can you undo/return or do you have to enter the code again ??Hi I just started with C-plus-plus a few days ago and know how hard it is to learn what you want to do when you don't really know what you are trying to find out how to do :)I think this will help you though, try this code:#include <iostream>using namespace std;int x=100;int y=10;int main(){char answer;char answer1;bool bContinue = false;do {cout << "Name:";cout << "Jack" << endl;cout << "Health:";cout << x << endl;cout << ""<< endl;cout << ""<< endl;cout << ""<< endl;cout << "Fight? Press 1" << endl;cout << "Sleep? Press 2" << endl;cin >> answer;if (answer == '1'){cout << "Get ready to fight a RABBIT !!!" << endl;cout << "Ready ??? Press 1" << endl;cout << "Return ??? Press 2" << endl;cin  >> answer1;if(answer1 == '1'){ bContinue = true;}}if (answer == '2'){cout << "ZZZZZZZzzzzzzzzzzz ZZZZZZZZZZZzzzzzz" << endl;}}while(!bContinue);  bool bRabbitDead = false;do{cout << "Rabbit has:" << endl;cout << y ;cout << "health" << endl;    cout << "system pause here, press any key to kill the rabit" << endl;    system("pause");bRabbitDead = true;} while(!bRabbitDead);cout << "Rabbit is dead " << endl; return 0;} All I added was 2 do while loops and 2 boolean variables.if you don't know what a boolean variable is, it's just a variable that holdsthe value either 'true' or 'false' the first thing i did was make a boolean variable called bContinue and set it to false.next I put the first question into a do while loop like this:do{ //this code} while (!bContinue);the above says while the variable bContinue is false, keep repeating the code.so theres no confusion, while(!bContinue) is just like saying while(bContinue == false)now after they choose to fight the rabbit you wanted to give them a chance to change their mind, so if they press 1 the loop will end and it will continue on to the next part of the code.  If anything else is pushed it will do the loop over again and ask them if they want to fight or sleep. if (answer == '1'){cout << "Get ready to fight a RABBIT !!!" << endl;cout << "Ready ??? Press 1" << endl;cout << "Return ??? Press 2" << endl;cin  >> answer1;    if(answer1 == '1'){ bContinue = true;}}after this I just did the same thing and made another do{}while(); with another boolean variable 'bRabbitDead'.  keep at it :)   ;  edit:: deleted::: double posted by mistake.. this forum has some lag to it when I try to submit a post.. sometimes it sits for 5 - 10 minutes..   ;  Thanks i think this is what im looking for Q2,AstralAaroN ill try it when i turn my laptop on. For Q1 i tried all the posts (i think....) but i cant seem to get it to work. Ill have a try again as i said when i turn my laptop, on so hopefully i will get it to work.Thanks for the responses, ill post back soon.   ;Quote:Original post by xiva For Q1 i tried all the posts (i think....)in your code you haveint main(){}my understanding is that 'int' infront of main()is saying that the main function is going to return an integer.at the bottom of your main function make sure you have this:int main(){return 0;}[Edited by - AstralAaroN on May 30, 2009 8:39:16 AM];  main is special, the return 0; is implicit if you don't write one yourself.See §3.6.2 Paragraph 5 of the C++ Standard.   