I tried debugging as well and outputted both the coin face and the toss selection, right before the if loop. The coin face would show as heads and the toss selection would be headsa or tailsb. (for context in the program, a is for heads and tails is for b). i tried flushing out the previous input by taking the input using getline and cin.ignore() as well but that also didn't work. Lastly else if (coinFace!=tossSelection) is working just fine which confused me even more?
Where the problem is occurring:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
if (mood=="good"){
cout<<"\nYou have chosen to flip a coin. The goblin grumbles but holds up to his end of the bargain\nDo you pick\n(a) heads OR\n(b) tails?\n\n> ";
cin>>tossSelection;
srand((unsignedint)time(NULL));
string coinFaces[2]={"heads", "tails"};
string coinFace = coinFaces[rand() % 2];
cout<<"\nThe coin is tossed and it comes up as "<<coinFace<<endl;
//the if statement not working
if(coinFace==tossSelection){
cout<<"\nDo you attack or defend
//rest of the stuff inside it
//if statement that IS working
else if(coinFace!=tossSelection){
combatGoblin();
}
void roomMonster(){
cout<<"\nFighting with goblin is commencing. Press enter to continue\n";
cin.ignore(std::numeric_limits<streamsize>::max(),'\n');
cout<<"The goblin won't let you go first unless you flip a coin with him. Do you: \n(a) concede and let the goblin go first OR \n(b) hope for the best and flip a coin?\n\n> ";
cin>>coinFlip;
if(coinFlip=='a'){
combatGoblin();
}
elseif (coinFlip=='b') {
string neutral;
srand((unsignedint)time(NULL));
string moods[2]={"good", "bad"};
string mood = moods[rand() % 2];
if (mood=="good"){
cout<<"\nYou have chosen to flip a coin. The goblin grumbles but holds up to his end of the bargain\nDo you pick\n(a) heads OR\n(b) tails?\n\n> ";
cin>>tossSelection;
srand((unsignedint)time(NULL));
string coinFaces[2]={"heads", "tails"};
string coinFace = coinFaces[rand() % 2];
cout<<"\nThe coin is tossed and it comes up as "<<coinFace<<endl;
if(coinFace==tossSelection){
cout<<"\nDo you attack or defend?\n\n> ";
cin>>actionChoice;
srand((unsignedint)time(NULL));
string actionProbabilities[2]={"pass", "fail"};
string actionProbability = actionProbabilities[rand() % 2];
if(actionProbability=="pass" && actionChoice=="attack"){
goblinHP=goblinHP-3;
cout<<"\nYour attack was successful. Goblin's HP was reduced by 3. \n\n*******************\n\nCurrent stats:\n\nYou -> "<<userHP<<" HP "<<userAP<<" AP"<<"\nGoblin -> "<<goblinHP<<" HP "<<goblinAP<<" AP\n\n*******************";
combatGoblin();
}
elseif(actionProbability=="fail" && actionChoice=="attack"){
cout<<"\nAttack failed. Nothing happened.";
combatGoblin();
}
elseif (actionProbability=="pass" && actionChoice=="defend"){
if(userHP<15){
userHP=userHP+1;
}
else {
cout<<"\nYou can not exceed 15 HP!";
combatGoblin();
}
}
elseif (actionProbability=="fail" && actionChoice=="defend"){
cout<<"\nDefend failed. Nothing happened.";
combatGoblin();
}
}
elseif(coinFace!=tossSelection){
combatGoblin();
}
}
elseif (mood=="bad"){
cout<<"\nThe goblin is in a bad mood. He has changed his mind about flipping the coin. Would you like to:\n(a) argue OR\n(b) let it go?\n\n> ";
cin>>argue;
if(argue=='a'){
userHP=userHP-1;
cout<<"\nThe goblin lashes out and scratches you. You have lost 1 HP. \n\n*******************\n\nCurrent stats:\n\nYou -> "<<userHP<<" HP "<<userAP<<" AP"<<"\nGoblin -> "<<goblinHP<<" "<<goblinAP<<" AP\n\n*******************";
combatGoblin();
}
elseif(argue=='b'){
combatGoblin();
}
}
}
}
srand() should only be used once at the beginning of main()
For the toss selection do you enter a or b or heads or tails? If you enter a or b then this won't equate to coinface which contains with heads or tails. You need something like (not tried):
1 2 3 4 5 6 7 8 9
cin>>tossSelection;
string coinFaces[2]={"heads", "tails"};
string coinFace = coinFaces[rand() % 2];
cout<<"\nThe coin is tossed and it comes up as "<<coinFace<<endl;
if (tossSelcection[0] == 'a' || tossSelection[0] == 'b')
tossSelection = coinFaces[tossSelection[0] - 'a'];
if(coinFace==tossSelection){