Program won't go inside of if statement

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((unsigned int)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();
			} 


Full method:

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
  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();
	} 
	else if (coinFlip=='b') {
		string neutral;
		srand((unsigned int)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((unsigned int)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((unsigned int)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();
				}
				else if(actionProbability=="fail" && actionChoice=="attack"){
					cout<<"\nAttack failed. Nothing happened.";
					combatGoblin();
				}
				else if (actionProbability=="pass" && actionChoice=="defend"){
					if(userHP<15){
						userHP=userHP+1;
					}
					else {
						cout<<"\nYou can not exceed 15 HP!";
						combatGoblin();
					}
				}
				else if (actionProbability=="fail" && actionChoice=="defend"){
					cout<<"\nDefend failed. Nothing happened.";
					combatGoblin();
				}
			}
			else if(coinFace!=tossSelection){
				combatGoblin();
			}
		}
		else if (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();
			}
			else if(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){

Topic archived. No new replies allowed.