I was writing a program to sim an attack and i set scbp to "sneak=N(string) FOA=N" and sneak_num(int) to 3.
1 2 3 4 5 6 7 8 9 10 11 12
if (scbp == "sneak=N FOA=N") {
if (sneak_num == 1,2,3,4,5)
cout << "Your sneak attack succeeds at getting into the village from the North side. They have the objective of raising the gate. Your other troops attack from the same side after the sneak attack and allow the sneak attack to enter unnoticed. The sneak attack raises the gate and you get in. You immediatly go after the leader and kill him. By killing the leader you became the new leader and all the other troops surrender.";
fmlead();}
if (scbp == "sneak=N FOA=N") {
if (sneak_num != 1,2,3,4,5) {
cout << "Your sneak attack gets shot at and dies. Your other troops attack from the same side after the sneak attack. Your soldiers can't break down the gate and die. You also die. Please try again."; } }
if (scbp == "sneak=S FOA=S") {
if (sneak_num == 1,2,3,4,5) {
cout << "Your sneak attack succeeds at getting into the village from the South side. They have the objective of raising the gate. Your other troops attack from the same side after the sneak attack and allow the sneak attack to enter unnoticed. The sneak attack raises the gate and you get in. You immediatly go after the leader and kill him. By killing the leader you became the new leader and all the other troops surrender.";
fmlead();}
}
Problem is it displays all 3 messages. These are 3 of the messages and i have 32 of them yet they don't load under the conditions. One is
1 2 3 4 5
if (scbp == "sneak=S FOA=S") {
if (sneak_num != 1,2,3,4,5) {
cout << "Your sneak attack succeeds at getting into the village from the South side. They have the objective of raising the gate. Your other troops attack from the same side after the sneak attack and allow the sneak attack to enter unnoticed. The sneak attack raises the gate and you get in. You immediatly go after the leader and kill him. By killing the leader you became the new leader and all the other troops surrender.";
fmlead();}
if(sneak_num == 1,2,3,4,5)
Only validates sneak_num == 1. I don't have a clue if the other values are even considered. The correct way to check multiple conditions: if(sneak_num == 1 || sneak_num == 2 || sneak_num == 3 /*...*/)
It may seem a bit lengthy, but a switch statement can quickly reduce the size.
"||" means "OR," which you can also use. It's opposite operator is "&&," which is AND. Comma operators don't work the same way.
A switch statement version of your code post:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
if(scbp == "sneak=N FOA=N"){ //Is this even allowed?
switch(sneak_num){
case 1: //Notice the empty cases; when one case is true
case 2: // everything will be executed until a "break"
case 3: // is reached.
case 4:
case 5:
cout << "Your sneak attack succeeds...." << '\n';
break;
default:
cout << "Your sneak attack gets shot at and dies...." << '\n';
break;
};
}