Seven Days coin toss Program

I need to write a program that recreates the Battle of Seven Days using Coin flips, but I cant seem to get the score to increment correctly

/* Program created by to recreate the Battle of Seven Days with coin flips.
The user inputs their guess for each battle At the end of the nine battles the victor is determined and the actual history,
of the Battle of Seven Days will be shown. */
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#include <iostream>
#include <cstdlib>
#include <ctime>4
#include <stdlib.h>
#include <cmath>
using namespace std;

// Used to generate the random number for coin toss
int coinToss(void)
{
    int randomNumber;
     randomNumber = 1 + rand() % 2;
     return randomNumber;

}

int main()
{

// String values to Hold Player guess = Guess, and the coin flip = toss.

string Guess;

string toss;



 int Heads, Tails;

 int randomNumber = 0;

string H = "H";
string T  = "T";
string t = "t";
string h = "h";

// Score keep

int  mcvictor;
int aivictor;

mcvictor = 0;
aivictor = 0;

//Coin flip value

 Heads = 1;
 Tails = 0;

//Main Program

cout <<"It is July 1st, 1862. The final day of the Battle of Seven Days."<<endl;
cout <<" You are general McClellan, in charge of the unionists, we will flip a coin to determine the victor of \n small territories in the battle."<<endl;



 cout << "Now General McClellan\n Do you Predict the Coin will land on Heads (H) or Tails (T)?"<<endl;
 cout << "Choose H or T."<<endl;
 cin >> Guess;

 //While statement for input verification

 while (Guess != H && Guess !=h && Guess!=T && Guess!=t)
 {
     cout<<"You have picked an invalid option, please Choose only 'H','h', 't', or 'T', without the single quotes.\n"<<endl;
     cin >>Guess;
 }

 //If statement for echo of player choice

if( Guess == H || Guess == h)
{
    cout << "You chose Heads"<<endl;
}

// echo of player choice

if  (Guess == T || Guess == t )
{
    cout << "You chose tails"<<endl;
}

    srand ((time(0))); //used to seed random number.
//Battle 1

for (int i = 1; i <=1 ; i++) // Get random number
{
    randomNumber = coinToss ();
    if (randomNumber == 1) //Give point value to "heads"
        toss = "Heads";
    else
        toss = "Tails"; // Give value to "tails"
    cout<<"Battle 1- Oak Groove."<<endl;
    cout << toss << " was flipped."<<endl;


}

//Needs re-done...Not incrementing correctly

if (toss == Guess)
{
    mcvictor++;

}
//Needs fixed....Not incrementing correctly.

else if (toss != Guess)
    {
        aivictor++;

    }


//Output user and AI score.
    cout <<"The score is: \n McClellan: "<<mcvictor<<endl;


    cout << "Lee: "<<aivictor<<endl;


    //Battle 2

     cout << "Choose H or T."<<endl;
 cin >> Guess;

 //While statement for input verification

 while (Guess != H && Guess !=h && Guess!=T && Guess!=t)
 {
     cout<<"You have picked an invalid option, please Choose only 'H','h', 't', or 'T', without the single quotes.\n"<<endl;
     cin >>Guess;
 }

 //If statement for echo of player choice

if( Guess == H || Guess == h)
{
    cout << "You chose Heads"<<endl;
}

// echo of player choice

if  (Guess == T || Guess == t )
{
    cout << "You chose tails"<<endl;
}


    for (int i = 1; i <=1 ; i++) // Get random number
{
    randomNumber = coinToss ();
    if (randomNumber == 1) //Give point value to "heads"
        toss = "Heads";
    else
        toss = "Tails"; // Give value to "tails"
    cout<<"Battle 2- Mechanicsville."<<endl;
    cout << toss << " was flipped."<<endl;
}

    return 0;
}

Last edited on
Please edit your code and use code tags - http://www.cplusplus.com/articles/jEywvCM9/
Sorry, I am new to posting, I did as you said.
@Akroncs

One big problem, is you set toss to equal "Tails" or "Heads", yet Guess can only be "T", "t", "H" or "h". Single characters. The two variables will never match. You could set the toss variable to a single character. That won't fully fix everything, but it's a start.
I have to make it so It displays "Heads" when h or H is pressed, I am unsure on what else I can do for that to happen.
@Akroncs

I fixed it up to display the "Heads" or "Tails", choices. But, since you're repeating a lot of the code, ie: for each battle, it would be wise to use as a function that gets called each time, instead. Will really make the program smaller, and easier to follow.

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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
// 7 Days Coin Toss.cpp : main project file.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
#include <cmath>
#include <string>

using namespace std;

// Used to generate the random number for coin toss
int coinToss(void)
{
	int randomNumber;
	randomNumber = rand() % 2;
	return randomNumber;

}

int main()
{

	// String values to Hold Player guess = Guess, and the coin flip = toss.
	srand((time(0))); //used to seed random number.
	string Guess;
	string toss;
	
	int Heads, Tails;
	int randomNumber = 0;

	string H = "H";
	string T = "T";
	
	// Score keep

	int  mcvictor = 0;
	int aivictor = 0;

	//Coin flip value

	Heads = 1;
	Tails = 0;

	//Main Program

	cout << "It is July 1st, 1862. The final day of the Battle of Seven Days." << endl;
	cout << " You are general McClellan, in charge of the unionists, we will flip a coin to determine the victor of \n small territories in the battle." << endl;
	
	cout << "Now General McClellan\n Do you Predict the Coin will land on Heads (H) or Tails (T)?" << endl;
	cout << "Choose H or T." << endl;
	cin >> Guess;
	
	//While statement for input verification

	while (Guess != "H" && Guess != "h" && Guess != "T" && Guess != "t")
	{
		cout << "You have picked an invalid option, please Choose only 'H' or 'T', without the single quotes.\n" << endl;
		cin >> Guess;
	}

	//If statement for echo of player choice

	if (Guess == "H" || Guess == "h")
	{
		cout << "You chose Heads" << endl;
		Guess = "Heads";
	}

	// echo of player choice

	if (Guess == "T" || Guess == "t")
	{
		cout << "You chose tails" << endl;
		Guess = "Tails";
	}

	
	//Battle 1
	//
	for (int i = 1; i <= 1; i++) // Get random number
	{
		//Don't really need a for loop, since you only call 
                // for a random number once
               randomNumber = coinToss();
		if (randomNumber == 1) //Give point value to "heads"
			toss = "Heads";
		else
			toss = "Tails"; // Give value to "tails"
		cout << "Battle 1- Oak Groove." << endl;
		cout << toss << " was flipped." << endl;
	}
	
	//Needs re-done...Not incrementing correctly

	if (toss == Guess)
	{
		mcvictor++;
	}
	//Needs fixed....Not incrementing correctly.

	else if (toss != Guess)
	{
		aivictor++;
	}

	//Output user and AI score.
	cout << "The score is: \n McClellan: " << mcvictor << endl;
	cout << " Lee: " << aivictor << endl;

	//Battle 2

	cout << "Choose H or T." << endl;
	cin >> Guess;

	//While statement for input verification

	while (Guess != "H" && Guess != "h" && Guess != "T" && Guess != "t")
	{
		cout << "You have picked an invalid option, please Choose only 'H','h', 't', or 'T', without the single quotes.\n" << endl;
		cin >> Guess;
	}

	if (Guess == "H" || Guess == "h")
	{
		cout << "You chose Heads" << endl;
		Guess = "Heads";
	}

	// echo of player choice

	if (Guess == "T" || Guess == "t")
	{
		cout << "You chose tails" << endl;
		Guess = "Tails";
	}

	//If statement for echo of player choice

	for (int i = 1; i <= 1; i++) // Get random number
       {
           	randomNumber = coinToss();
		if (randomNumber == 1) //Give point value to "heads"
			toss = "Heads";
		else
			toss = "Tails"; // Give value to "tails"
		cout << "Battle 2- Mechanicsville." << endl;
		cout << toss << " was flipped." << endl;
	}

	if (toss == Guess)
	{
		mcvictor++;
	}
	
	else if (toss != Guess)
	{
		aivictor++;
	}

	//Output user and AI score.
	cout << "The score is: \n McClellan: " << mcvictor << endl;
	cout << " Lee: " << aivictor << endl;
	return 0;
}
Thank you so much. I am new to coding and c++, this has been bugging me for awhile now.

If I were to use a loop to work for each battle without repeating code what would I do?
@Akroncs

Don't really have time at the present to create a the function right now, but will post a bit of it, later on in Thursday.
That would be great!
There are a total of nine battles, each with their own name.
@Akroncs

Here's using for loops and functions. The rest is up to you..

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
// 7 Days Coin Toss.cpp : main project file.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>
#include <cmath>
#include <string>

using namespace std;

// Used to generate the random number for coin toss
int coinToss()
{
	int randomNumber;
	randomNumber = rand() % 2;
	return randomNumber;
}

int Battles()
{
	string Guess;
	
	cout << "Now, General McClellan\nDo you Predict the Coin will land on Heads (H) or Tails (T)?" << endl;
	cout << "Enter an H or a T." << endl;
	cin >> Guess;

	

	//While statement for input verification

	while (Guess != "H" && Guess != "h" && Guess != "T" && Guess != "t")
	{
		cout << "You have picked an invalid option, please Choose only an 'H' or a 'T',\nwithout the single quotes.\n" << endl;
		cin >> Guess;
	}

	//If statement for echo of player choice

	if (Guess == "H" || Guess == "h")
	{
		cout << "You chose Heads." << endl;
			return 1;
	}

	// echo of player choice

	if (Guess == "T" || Guess == "t")
	{
		cout << "You chose Tails." << endl;
	      return 0;
	}
	return -1;
}

int main()
{

	// String values to Hold Player guess = Guess, and the coin flip = toss.
	srand((time(0))); //used to seed random number.
	string Guess;
	string toss;
	
	int Heads, Tails;
	int randomNumber, coin;
	string Battle_Names[9] = { "Oak Groove", "Mechanicsville", "Battle 3", "Battle 4", "Battle 5", "Battle 6", "Battle 7", "Battle 8", "Battle 9" };
	
	// Score keep

	int  mcvictor = 0;
	int aivictor = 0;

	//Coin flip value

	Heads = 1;
	Tails = 0;

	//Main Program

	cout << "It is July 1st, 1862. The final day of the Battle of Seven Days." << endl;
	cout << " You are general McClellan, in charge of the unionists, we will flip a coin to determine the victor of \n small territories in the battle." << endl;
	
	for (int x = 0; x < 3; x++) // Loop only first 3
	{
		cout << endl << "Battle " << x + 1 << " - " << Battle_Names[x] << "." << endl; // Prints battle # and battle name
		randomNumber = coinToss();
		coin = Battles();
		if (randomNumber == 1)
			toss = "Heads";
		else
			toss = "Tails"; // Give value to "tails"
		
		cout << toss << " was flipped." << endl;
		
		if (randomNumber == coin)
		{
			mcvictor++;
		}
		else
		{
			aivictor++;
		}

		//Output user and AI score.
		cout << "The score is: \n McClellan : " << mcvictor << endl;
		cout << " Lee : " << aivictor << endl;
	}
	return 0;
}
@whitenite1

Thank you so much for all of your help!
Topic archived. No new replies allowed.