Sign in to follow this  
xiva

(c++) 2 noob questions ! :)

Recommended Posts

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 ??

Share this post


Link to post
Share on other sites
First change
"char answer;
char answer1;
"

to int answer,answer1;

the take the user input like this :

if(answer == 1) {cout<<"answer1"<<endl;}; else
if(answer == 2) { cout<<"answer2"<<endl;};
and so on.

what do you mean by your question#2? Can you be a little more clearer?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
#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.

Share this post


Link to post
Share on other sites
Quote:
Original post by xiva

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 ??



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 holds
the 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 :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Quote:
Original post by xiva
For Q1 i tried all the posts (i think....)


in your code you have

int 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]

Share this post


Link to post
Share on other sites
So if i used int //something else i could not include return 0; and it would jsut not close ?? or am i mis-understanding what your trying to say ??

Share this post


Link to post
Share on other sites
I got it working well no... You lot got it working i haven't been feeling great lately and mis-read the post/s and mis-understood the post/s about using cin >> answer so people who said that thanks.

Fixed it all now, so thanks very helpful and quick responses thanks for the 3'rd time this post.

Share this post


Link to post
Share on other sites
Also, you can chain together some of your ostream insertions like this:


cout << "Name:" << "Jack" << endl;
cout << "Health:" << x << endl;
cout << ""<< endl;
cout << ""<< endl;
cout << ""<< endl;
cout << "Fight? Press 1" << endl;
cout << "Sleep? Press 2" << endl;
cin >> answer;

or alternatively...

cout << "Name:" << "Jack" << endl << "Health:" << x << endl
<< "" << endl << "" << endl << "" << endl
<< "Fight? Press 1" << endl << "Sleep? Press 2" << endl;
cin >> answer;




I sometimes do it like that because it makes fewer lines to scroll through.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this