I'm making an rpg style game and when you go into the inventory screen and say equip whatever you put it goes to this one else if statement nothing else and i have other else if/else statements i need it to see. Heres the code portion:
else if(choice15 == "equip") {
cout << "What do you want to equip(knife,sword,battleaxe): ";
string choice16;
cin >> choice16;
if (choice16 == "knife" && inventory[0] == "knife" || inventory[1] == "knife" || inventory[2] == "knife" || inventory[3] == "knife" || inventory[4] == "knife" || inventory[5] == "knife" || inventory[6] == "knife" || inventory[7] == "knife" || inventory[8] == "knife" || inventory[9] == "knife") {
system("CLS");
equipment = "knife";
cout << "You equipped a knife\n";
cout << "Would you like to go back to the main menu(yes/no): ";
cin >> choice17;
if (choice17 == "yes") {
system("CLS");
PlayAgain = true;
}
Sorry and, my question was how do i get it to stop going to the else if where it says you don't have a knife because if i put sword it will print you don't have a knife and it should print you don't have a sword. So heres the code again :
You can compress ALL of this code by changing it to:
1 2 3 4 5 6 7 8 9 10 11
elseif(choice15 == "equip") {
cout << "What do you want to equip(knife,sword,battleaxe): ";
string choice16;
cin >> choice16;
if(choice16 == inventory[0] || choice16 == inventory[1] || choice16 == inventory[2]/*... You Get The Idea*/)
{cout << "You equiped a " << choice16 << '\n';
/*Code Code Code*/}
else
{cout << "You don't have a " << choice16 << '\n';
/*Code Code Code*/}
Now, isn't that more simple to read?
EDIT: There are ways to shorten this even more, but at that point you may not recognize your own code and it would take us too far off topic. See if shortening it like this helps you at all.
Fixing errors is never off-topic (a stylistic one in this case).
It should be written as if (find(inventory.begin(),inventory.end(),choice16)!=inventory.end())cout << " You have [...]
It would be even better to create an inventory class and provide a function "contains" to avoid all of this awful code duplication.
Also, I cannot find a dedicated reference but "X.begin()" and "X.end()" are built in member functions as well. This is part of what I meant about shortening it even more, you would benifit a great deal by creating objects for your items in the form of a class.