My code is repeating a part I don't want repeated...

This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
cout << "\n Your rival appears! What is his name?" <<endl;
	cin >> rivalname;
	cout << "\n " << rivalname << " would like to fight! Send out " <<nickname << "? (y/n)" <<endl;
	cin >> rival_answer1;
	if (rival_answer1=="y");
	{
		cout << "\n You send out " << nickname << "." <<endl;
		cout << "\n Fight (1) or Run (2)?" <<endl;
		cin >> battle1;
		if (battle1=="1")
		{
			cout << "\n " <<nickname << " knocked out " << rivalname << " in one hit!" <<endl;
			cout << "\n You won!" <<endl;
			cout << "\n " <<rivalname << " handed over $200" <<endl;
		}
		if (battle1=="2");
		{
			cout << "\n Invalid option, game over!" <<endl;
		}
	}
	if (rival_answer1=="n");
	{
		cout << "\n You have no other pokemon! Invalid option, you send out " << nickname << " anyways!" <<endl;
		cout << "\n Will you fight (1) or Run (2)?" <<endl;
		cin >> battle1;
		if (battle1=="1");
		{
			cout << "\n " <<nickname << " knocked out " << rivalname << " in one hit!" <<endl;
			cout << "\n You won!" <<endl;
			cout << "\n " <<rivalname << " handed over $200" <<endl;
		}
		if (battle1=="2");
		{
			cout << "\n Invalid option, game over!" <<endl;
		}
	}
	system("pause");
	return 1;
}


My program works great until this portion of the code, and when I press y or n it goes to the right option, but then when I press 1 or 2 it whacks out and does weird stuff. Like it gives me the right answer but then also gives me the answer for the option I didn't choose. Any help?
Um.. I think you want some else if's up there. Also I hope rival_answer1 and battle1 are strings and not char* because otherwise those if statements will not do what you expect.
Last edited on
What does else if do instead of if? I've only used if for all my other choices and this hasn't happened. Ya, all of those are strings, I'm not actually sure what a char* is.
char* is


A char* is a pointer to a char, in this case he means an array to a pointer of a few chars. If you need to learn some basics of native C++, the tutorial on this site is good.
Well I have gone thru a number of them, but I skimmed them. I tried the else if thing, but it didn't change anything and now the program won't work at all. Says something about not linking the else if to an if.
I'm really not sure what the problem is, the code looks fine to me (so far.) Can you post the declaration of those variable your using in this section? Also what exactly is the suspicions output; post what you are getting back from the program.

As for else if's, make sure you understand the following code:
1
2
3
4
5
6
bool ur_awsome;
//Initializing variables...
if( ur_awsome )
   cout << "Woot woot!";
else
   cout << "Awww...";


Now examine the difference:
1
2
3
4
5
6
bool cplusplus_rocks, ifs_rock;
//Initializing variables...
if( cplusplus_rocks )
   cout << "These are weird looking rocks...";
if( ifs_rock )
   cout << "Hypothetical terrain?";
(cplusplus_rocks, ifs_rock)
(true, true) These are weird looking rocks...Hypothetical terrain?
(true, false) These are weird looking rocks...
(false, true) Hypothetical terrain?
(false, false)

1
2
3
4
5
6
bool cplusplus_rocks, ifs_rock;
//Initializing variables...
if( cplusplus_rocks )
   cout << "These are weird looking rocks...";
else if( ifs_rock )
   cout << "Hypothetical terrain?";
(cplusplus_rocks, ifs_rock)
(true, true) These are weird looking rocks...
(true, false) These are weird looking rocks...
(false, true) Hypothetical terrain?
(false, false)


iggysmartbomb wrote:
I'm not actually sure what a char* is.
char*, char[], if your using strings don't worry about it.
Last edited on
There is no difference? So it shouldn't matter whether I am using if or else if?
Look closer at the possible output (to the right of the code.)
Oh I see, when both are true both are displayed when only using ifs, but with else if one is displayed. So I tried to use else if, but it says I need to link my else if to an in or something like that. How do I link them?
Your code should look something like this using else-ifs:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
...
if (rival_answer1=="y") //This is the outer if block
{
	...
	if (battle1=="1") //This is an inner if block
	{
		...
	}
	else if (battle1=="2"); //This else ties to the above inner if block.
	{
		...
	}
}
else if (rival_answer1=="n") //The else ties to the outer if block
{
	...
	if (battle1=="1"); //This is another inner if block.
	{
		...
	}
	else if (battle1=="2"); //This else ties to the inner if block directly above.
	{
		...
	}
}
...


Also check lines 5 and 21 in your code. You need to remove the semicolons after the if statements!
Last edited on
And now it works! Finally! Thanks so much for the help, adding in else if's did the trick. Time to continue with my project.
No problem. Just think about it like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
//If you want to do something only if some condition is true, you want "if"
if( something )
   //...

//if you want to do something if a condition is true and something else if it's not, you want "if, else"
if( something )
   //...
else
   //...

//If you want to pick only one choice based on many conditions, you want "if, else if"
if( something )
   //...
else if( somethingElse )
   //...
else if( yetSomethingElse )
   //...
...
else //optional, default fall though code if all above conditions fail
   //... 
Last edited on
Alright thanks, I appreciate your help.
Topic archived. No new replies allowed.