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 elseif'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.
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.
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 elseif's, make sure you understand the following 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?
...
if (rival_answer1=="y") //This is the outer if block
{
...
if (battle1=="1") //This is an inner if block
{
...
}
elseif (battle1=="2"); //This else ties to the above inner if block.
{
...
}
}
elseif (rival_answer1=="n") //The else ties to the outer if block
{
...
if (battle1=="1"); //This is another inner if block.
{
...
}
elseif (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!
//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 )
//...
elseif( somethingElse )
//...
elseif( yetSomethingElse )
//...
...
else //optional, default fall though code if all above conditions fail
//...