Advertisement

Another newbie question

Started by March 22, 2002 10:44 AM
6 comments, last by Mage_gr 22 years, 7 months ago
Guys in the following program when I insert the number two it goes ok.But when I put anything else it shows all the other possibilities help!!!! #include <iostream.h> int game() { int first; cout<<"You are in a locked room full of stores for the soldiers of the base.\nWhat are you going to do?Enter a number.Enter 1 in order to try to force the door.\nEnter 2 in order to try to go through the ventilation.\nEnter 3 in order to pretend to be ill and call for the guard to check.\nEnter:"; cin>> first; switch(first) { case 1:cout<<"The door is made of titanium and has a really tuff lock.\nYou don''t think you can open it.GAME OVER"; break; case 2:cout<<"Yeah that''s it you made it through the ventilation.Thank god you were on a diet before you were captured."; gamea(); case 3:cout<<"Nope.Didn''t work.The guard understood your trick and started laughing.You must try something else.GAME OVER"; break; default:cout<<"Wrong number"; } return first; } int gamea() { int second; cout<<"Good you made it through the ventilations.Now you are in a hall.\nYou can either go right or left.Enter 1 for right or 2 for left.Enter:"; cin>> second; switch(second) { case 1:cout<<"Right didn''t appear to be the right choice, a surveilance \ncamer caught a glimpse of you.That was it the guards caught\n you.GAME OVER"; break; case 2:cout<<"Gongratulations.You are really good at this.An idiot must\n have left this way unguarded.You made your way\n out of the prison facility of the base."; gameb(); default:cout<<"Wrong number"; } return second; } int gameb() { int third; cout<<"You are now in the Prison yard.You approach the exit door\n but see that it requires a password.\nFortunately it gives you a hint.\nThe password is (2*2*8)-30.\nEnter password:"; cin>> third; switch(third) { case 2:cout<<"Oh boy you are good.You found the password.You made it\n completely out of the prison and into the\nmain base."; break; default:cout<<"Wrong password.GAME OVER."; } return third; } int main() { cout<<"eeeeeee\n"; cout<<"eeeeeee\n"; cout<<"ee\n"; cout<<"ee\n"; cout<<"ee\n"; cout<<"ee\n"; cout<<"eeeeeee 11111\n"; cout<<"eeeeeee 11 11\n"; cout<<"ee 11 11\n"; cout<<"ee 11\n"; cout<<"ee 11\n"; cout<<"ee 11\n"; cout<<"eeeeeee 11\n"; cout<<"eeeeeee 11\n"; if(game()==2) { if(gamea()==2); } { gameb(); } return 0; }
When you see a roller coaster, get on it,put your hands in the air,and ride it to the very end.Life doesn't remember you unless you kick,scream,and claw your way to the top.There is nothing in the world that is impossible.If you believe that you can do it, you will.
Your ''if'' statements at the bottom are a little off.

if(game() == 2)
{
if(gamea() == 2)
{
gameb();
}
}

Try this out.
Advertisement
you are missing lots of break;''s on your switch statements

Two previous answers are correct.

This code works:


  #include <iostream.h>int game();int gamea();int gameb();int game(){	int first;	cout << endl << endl;	cout << "You are in a locked room full of stores for the soldiers of the base." << endl; 	cout << "What are you going to do? Enter a number." << endl;  	cout << "Enter 1 in order to try to force the door." << endl;  	cout << "Enter 2 in order to try to go through the ventilation." << endl;   	cout << "Enter 3 in order to pretend to be ill and call for the guard to check." << endl << endl;    cout << "Enter: ";	cin >> first;	switch(first)	{		case 1:  			cout << "The door is made of titanium and has a really tuff lock." << endl;     		cout << "You don't think you can open it." << endl;         	cout << "GAME OVER";		break;		case 2:  			cout << "Yeah that's it you made it through the ventilation." << endl;     		cout << "Thank god you were on a diet before you were captured.";		break;		case 3:  			cout << "Nope. Didn't work. The guard understood your trick and started laughing." << endl;     		cout << "You must try something else." << endl;         	cout << "GAME OVER";		break;		default:  			cout << "Wrong number";		break;	}	return first;}int gamea(){	int second;	cout << endl << endl;	cout << "Good you made it through the ventilations. Now you are in a hall." << endl; 	cout << "You can either go right or left." << endl;  	cout << "Enter 1 for right or 2 for left." << endl;   	cout << "Enter: ";	cin >> second;		switch(second)	{		case 1:  			cout << "Right didn't appear to be the right choice, a surveilance" << endl;     		cout << "camer caught a glimpse of you. That was it the guards caught you." << endl;         	cout << "GAME OVER";		break;		case 2:  			cout << "Congratulations. You are really good at this." << endl;     		cout << "An idiot must have left this way unguarded." << endl;       		cout << "You made your way out of the prison facility of the base.";		break;		default:  			cout << "Wrong number";		break;	}	return second;}int gameb(){	int third;	cout << endl << endl;	cout << "You are now in the Prison yard. You approach the exit door" << endl; 	cout << "but see that it requires a password." << endl;  	cout << "Fortunately it gives you a hint." << endl;   	cout << "The password is (2*2*8)-30." << endl;   	cout << "Enter password: ";	cin >> third;		switch(third)	{		case 2:  			cout << "Oh boy you are good. You found the password. You made it" << endl;     		cout << "completely out of the prison and into the main base.";		break;		default:  			cout<<"Wrong password.";         	cout << "GAME OVER";    	break;	}	return third;}int main(){	cout << "eeeeeee" << endl;	cout << "eeeeeee" << endl;	cout << "ee" << endl;	cout << "ee" << endl;	cout << "ee" << endl;	cout << "ee" << endl;	cout << "eeeeeee 11111" << endl;	cout << "eeeeeee 11 11" << endl;	cout << "ee 11 11" << endl;	cout << "ee 11" << endl;	cout << "ee 11" << endl;	cout << "ee 11" << endl;	cout << "eeeeeee 11" << endl;	cout << "eeeeeee 11" << endl;	if(game() == 2)	{		if(gamea() == 2)  			gameb();	}		return 0;}   



[edited by - Pepe on March 22, 2002 12:36:33 PM]
You will need to add a break after calling gamea() in case 2 of the int game() func. Otherwise when execution leaves the scope of gamea() it will return to game() and fall through to case 3.

Err you will also need to do the same thing after calling gameb() in case 2 of gamea().


[edited by - dviator on March 22, 2002 12:38:47 PM]
thanks but I understand that if I continue this way I will get full of if staements.Please tell me another way to do it properly.
When you see a roller coaster, get on it,put your hands in the air,and ride it to the very end.Life doesn't remember you unless you kick,scream,and claw your way to the top.There is nothing in the world that is impossible.If you believe that you can do it, you will.
Advertisement
also what is this:<<end1
When you see a roller coaster, get on it,put your hands in the air,and ride it to the very end.Life doesn't remember you unless you kick,scream,and claw your way to the top.There is nothing in the world that is impossible.If you believe that you can do it, you will.
Read Sabre''s post on your other topic:
http://www.gamedev.net/community/forums/topic.asp?topic_id=86297

You are moving forward in game design ...you have figured out that if you keep with your current design, your program will be a huge ifelse, ifelse... program.

Sabre gives you a better design.

This topic is closed to new replies.

Advertisement