If,Elseif problem

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;
}

else {
exit(0);
}
}

else 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") {
cout << "You don't have a knife" << endl;
cout << "Go back to main menu(yes/no): ";
cin >> choice18;

if(choice18 == "yes") {
system("CLS");
PlayAgain = true;
}

else {
exit(0);
}

}


else if (choice16 == "sword" && inventory[0] == "sword" || inventory[1] == "sword" || inventory[2] == "sword" || inventory[3] == "sword" || inventory[4] == "sword" || inventory[5] == "sword" || inventory[6] == "sword" || inventory[7] == "sword" || inventory[8] == "sword" || inventory[9] == "sword") {
system("CLS");
equipment = "sword";
cout << "You equipped a sword" << endl;
cout << "Go back to main menu(yes/no): ";
cin >> choice19;

if (choice19 == "yes") {
system("CLS");
PlayAgain = true;
}

else {
exit(0);
}
}

else if (choice16 == "sword" && inventory[0] != "sword" || inventory[1] != "sword" || inventory[2] != "sword" || inventory[3] != "sword" || inventory[4] != "sword" || inventory[5] != "sword" || inventory[6] != "sword" || inventory[7] != "sword" || inventory[8] != "sword" || inventory[9] != "sword") {
cout << "You don't have a sword" << endl;
cout << "Go back to the main menu(yes/no): ";
cin >> choice20;

if (choice20 == "yes") {
system("CLS");
PlayAgain = true;
}

else {
exit(0);
}
}

else if (choice16 == "battleaxe" && inventory[0] == "BattleAxe" || inventory[1] == "BattleAxe" || inventory[2] == "BattleAxe" || inventory[3] == "BattleAxe" || inventory[4] == "BattleAxe" || inventory[5] == "BattleAxe" || inventory[6] == "BattleAxe" || inventory[7] == "BattleAxe" || inventory[8] == "BattleAxe" || inventory[9] == "BattleAxe") {
inventory[numitems++] = "BattleAxe";
cout << "You equipped a battle axe\n";
cout << "Go back to main menu(yes/no): ";
cin >> choice21;

if (choice21 == "yes") {
system("CLS");
PlayAgain = true;
}

else {
exit(0);
}

}

else if (choice16 == "battleaxe" && inventory[0] != "BattleAxe" || inventory[1] != "BattleAxe" || inventory[2] != "BattleAxe" || inventory[3] != "BattleAxe" || inventory[4] != "BattleAxe" || inventory[5] != "BattleAxe" || inventory[6] != "BattleAxe" || inventory[7] != "BattleAxe" || inventory[8] != "BattleAxe" || inventory[9] != "BattleAxe") {
cout << "You don't have a battle axe\n";
cout << "Go back to main menu(yes/no): ";
cin >> choice22;

if (choice22 == "yes") {
system("CLS");
PlayAgain = true;
}

else {
exit(0);
}

}

else {
cout << "Invalid choice" << endl;
PlayAgain = true;
}



}

else {
cout << "Invalid choice" << endl;
PlayAgain = true;
}




break;
What was your question again? And also, your code goes [code ]here[ /code] not here.
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 :


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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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;
}

else {
exit(0);
}
}

else 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") {
cout << "You don't have a knife" << endl;
cout << "Go back to main menu(yes/no): ";
cin >> choice18;

if(choice18 == "yes") {
system("CLS");
PlayAgain = true;
}

else {
exit(0);
}

}


else if (choice16 == "sword" && inventory[0] == "sword" || inventory[1] == "sword" || inventory[2] == "sword" || inventory[3] == "sword" || inventory[4] == "sword" || inventory[5] == "sword" || inventory[6] == "sword" || inventory[7] == "sword" || inventory[8] == "sword" || inventory[9] == "sword") {
system("CLS");
equipment = "sword";
cout << "You equipped a sword" << endl;
cout << "Go back to main menu(yes/no): ";
cin >> choice19;

if (choice19 == "yes") {
system("CLS");
PlayAgain = true;
}

else {
exit(0);
}
}

else if (choice16 == "sword" && inventory[0] != "sword" || inventory[1] != "sword" || inventory[2] != "sword" || inventory[3] != "sword" || inventory[4] != "sword" || inventory[5] != "sword" || inventory[6] != "sword" || inventory[7] != "sword" || inventory[8] != "sword" || inventory[9] != "sword") {
cout << "You don't have a sword" << endl;
cout << "Go back to the main menu(yes/no): ";
cin >> choice20;

if (choice20 == "yes") {
system("CLS");
PlayAgain = true;
}

else {
exit(0);
}
}

else if (choice16 == "battleaxe" && inventory[0] == "BattleAxe" || inventory[1] == "BattleAxe" || inventory[2] == "BattleAxe" || inventory[3] == "BattleAxe" || inventory[4] == "BattleAxe" || inventory[5] == "BattleAxe" || inventory[6] == "BattleAxe" || inventory[7] == "BattleAxe" || inventory[8] == "BattleAxe" || inventory[9] == "BattleAxe") {
inventory[numitems++] = "BattleAxe";
cout << "You equipped a battle axe\n";
cout << "Go back to main menu(yes/no): ";
cin >> choice21;

if (choice21 == "yes") {
system("CLS");
PlayAgain = true;
}

else {
exit(0);
}

}

else if (choice16 == "battleaxe" && inventory[0] != "BattleAxe" || inventory[1] != "BattleAxe" || inventory[2] != "BattleAxe" || inventory[3] != "BattleAxe" || inventory[4] != "BattleAxe" || inventory[5] != "BattleAxe" || inventory[6] != "BattleAxe" || inventory[7] != "BattleAxe" || inventory[8] != "BattleAxe" || inventory[9] != "BattleAxe") {
cout << "You don't have a battle axe\n";
cout << "Go back to main menu(yes/no): ";
cin >> choice22;

if (choice22 == "yes") {
system("CLS");
PlayAgain = true;
}

else {
exit(0);
}

}

else {
cout << "Invalid choice" << endl;
PlayAgain = true;
}



}

else {
cout << "Invalid choice" << endl;
PlayAgain = true;
}




break; 
Anyone?
You can compress ALL of this code by changing it to:

1
2
3
4
5
6
7
8
9
10
11
else if(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.
Last edited on
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.
@ OP: NOTE: You don't need to write the "find(...)" function in Athars example. See here: http://www.cplusplus.com/reference/algorithm/find/

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.
Thank you all of your tips solved the problem. Especially computergeeks tips thanks alot everyone!
@ OP: You're going to have problems with character case conversions unless you do something to standardize them. The way your code is, it will compile, but you're asking the user to type what they want to equip, and:
"battleaxe" != "BattleAxe" != "BATTLEAXE"
http://www.cplusplus.com/reference/algorithm/transform/
http://www.cplusplus.com/reference/clibrary/cctype/toupper/
http://www.cplusplus.com/reference/clibrary/cctype/tolower/
^These can help you out a bit.
Topic archived. No new replies allowed.