Stuck - Again: Coin Flip Game with functions

[sorry this is a long one] Hello, I know I'm becoming a regular here but I am just not getting it and I am so sorry to keep bugging you guys with what I'm sure are silly problems/questions. I'm new to C++ and programming all together and I decided to join an accelerated class to start with (regretting this decision now).

My problems/issues with this Coin Flip assignment is figuring out how how to build and call my functions appropriately them from main().

The instructions are as follows:
Write a program that starts a player off with a bank of $15.00. A coin will flip and randomly choose heads or tails. The user will guess heads or tails to win. If the coin flip matches the player's guess his bet will be doubled. It costs 1 dollar to play and the program will bet that amount automatically each time as long as there is the available bank amount. 

Note: Do not let the game begin if the user's bank amount is below $1.00. This is a large assignment. You should be using plenty of functions, random numbers, decision statements, the while loop(s) (not a do-while loop), unsigned variables, and i/o manipulation at a minimum.

At the end of the game, ask the user if he/she would like to play again and loop the game with bet input sequence again until the bank reaches 0.00 or the user inputs "n" to quit playing.


Here's what will earn point deductions:

Too few functions (This should have at least 3). Don't be afraid to use functions to help modularize the parts to this game.
Global variables ( no00000oooooooooooOOOOOoo!   -Darth Vader ) 
Too many win case evaluations. There should only be if you win or lose. Do not check against both heads and tails and this, and that. You will create a permutation of every outcome. This is not a smart approach. Think, how do you win? You win if your guess is == to the coin flip. I'll say no more.
Syntax errors
Improper logic or improper function arguments/return type
Duplication of code. If you're writing the same thing twice, you're not leveraging functions or proper logic.
Round peg, square hole. i.e. Don't use a for loop and try and convert it to a while loop. Use the right tool for the job. 


The completed program should look like the following:

Welcome to the coin flip game.
It will cost 1 dollar to play
If you guess correctly, you will match your bet one to one

Would you like to play? <y/n>
y
Guess heads or tails and I will tell you if guessed correctly <h/t>
h

The coin landed on Tails
I am sorry, but you did not win this time. 
Your bank balance is :$14.00

Would you like to play again? <y/n>
y
Guess heads or tails and I will tell you if you guess correctly <h/t>
t

The coin landed on Tails
YOU WIN $2.00
Your bank balance is :$16.00

Would you like to play again? <y/n>
n

Thank you for playing. Your bank balance is $16.00
Press any key to continue . . .





Last edited on
Forgot to insert my code.
Here is what I have so far and the errors I am getting:

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
#include <iostream>
#include <string>
#include <ctime>
#include <cmath>

using namespace std;

string CoinFlip();
bool bank();

int main()
{
	char ans, guess;
	double bank = 15.00, winnings = 0.0;

	unsigned seed = time(0);
	srand(seed);

	cout << "Welcome to the coin flip game." << endl;
	cout << "It will cost 1 dollar to play" << endl;
	cout << "If you guess correctly, you will match your bet one to one" << endl << endl;

	cout << "Would you like to play? (Y/N)" << endl;
	cin >> ans;

	if (ans == 'y' || ans == 'Y')
	{
		cout << "\nGuess heads or tails and I will tell you if you guessed correctly <h/t>" << endl;
		cin >> guess;
	}
	else
	{
		cout << "Thanks you for playing ."; 
		//cout << enter bank balance function
	}

	cout << "The coin landed on " << CoinFlip () << endl;


	

		if (coinResult == guess)
		{
			cout << "YOU WIN $2.00" << endl;
			winnings = 2.00;
			//cout << enter bank balance function
		}
		else
		{
			cout << "I am sorry, but you did not win this time." << endl;
			winnings = -2.00;
			//cout << bank balance function
		}
		bank += winnings;
	
		cout << "Your bank balance is: $ "; //enter bank balance funtion include cout statment in bank balance << endl;

		cout << "Would you like to play again? <Y/N>" << endl;

	return 0;
}

string CoinFlip( ) //flip funtion
{
	int coinResult = rand() % 2;

	if (coinResult == 0)
		return "Heads";
	else
		return "Tails";
}


bool bank()
{
	if (bank <= 0)
		return true;
	else
		return false;
}



I am not sure how to build an appropriate "bank balance" function, either have all out in main or build it as a separate function.

These are the errors I am currently getting:

identifier "coinResult" is undefined	
'coinResult': undeclared identifier         	


For the error messages, look at if (coinResult == guess)

The above code is trying to use a variable named coinResult that has not been declared. Since CoinFlip() returns a string, declare coinResult before the if statement and assign a value using CoinFlip();

1
2
3
4
string coinResult = CoinFlip();
cout << "The coin landed on " << coinResult << endl;
if (coinResult == guess)
...


For the bank() function what do you want it to do?
Thank you EtDecius

I'm trying to figure out how to get the bank function to add 2.00 (winnings) to the balance (starting at 15.00) when the guess is right and then also how to deduct 1.00 from the balance when the guess is wrong.

I know I should change winnings = 2.00 to winning = winnings + 2.00

I just don't know if I need to separate this as its own function or just leave this all in main?

My design and formatting for this project is scrambled up, I'm sorry

I made the following changes:
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
#include <iostream>
#include <string>
#include <ctime>
#include <cmath>

using namespace std;

string CoinFlip();
bool bankbroke();

int main()
{
	char ans, guess;
	double bank = 15.00, winnings = 0.0;

	
	unsigned seed = time(0);
	srand(seed);

	cout << "Welcome to the coin flip game." << endl;
	cout << "It will cost 1 dollar to play" << endl;
	cout << "If you guess correctly, you will match your bet one to one" << endl << endl;

	cout << "Would you like to play? (Y/N)" << endl;
	cin >> ans;

	string coinResult = CoinFlip();

	if (ans == 'y' || ans == 'Y')
	{
		cout << "\nGuess heads or tails and I will tell you if you guessed correctly <h/t>" << endl;
		cin >> guess;
	}
	else
	{
		cout << "Thanks you for playing ."; 
		//cout << enter bank balance function
	}
	
	cout << "The coin landed on " << coinResult << endl;	

		if (coinResult == guess)
		{
			cout << "YOU WIN $2.00" << endl;
			winnings = winnings + 2.00;
			//cout << enter bank balance function?
		}
		else
		{
			cout << "I am sorry, but you did not win this time." << endl;
			winnings = winnings - 1.00;
			//cout << bank balance function?
		}
		bank += winnings;
	
		cout << "Your bank balance is: $ "; //enter bank balance function? << endl;

		cout << "Would you like to play again? <Y/N>" << endl;

	return 0;
}

string CoinFlip( ) //flip funtion
{
	int coinResult = rand() % 2;

	if (coinResult == 0)
		return "Heads";
	else
		return "Tails";
}


bool bankbroke()
{
	if (bankbroke <= 0)
		return true;
	else
		return false;
}

/*double bankbalance(0.0) [bank balance function] not sure if I should use
{
	//Should i copy the if coinResult == guess and elstatements?
}
*/


Below are the errors:

no operator "==" matches these operands         line 42

binary'==': no operator found which takes left-hand operand of type 'std::string' (or there is no acceptable conversion)                    line42


coinResult is a string. Guess is a char. You cannot compare string to char with an equality operator.
gotcha, but I am not sure how to fix this. I need coinResult to be equal to guess to see that the player won or not....(did I say that right?)

I feel like I'm so close yet so far :(
Since the program example stores input as a char, you can modify CoinFlip() to return a char instead of a string. That will let you compare the results of CoinFlip() to the user's input using ==.

Since banking is such a simple operation, it may not need it's own function.

Couple things to note:

Your program should have a while loop so that the user can continue to play until they are done or out of money.

The while loop can be in main() or it's own function, but it should probably call other functions to do most of the work.

1
2
3
4
5
6
7
8
9
10
11
12
13
cout << "Would you like to play? (Y/N)" << endl;
cin >> ans;

while (bank >= 1.00 && (ans == 'y' || 'ans' == 'Y'))
{
    // Call function to get guess from user and store result in variable
    // Call CoinFlip() and store result in variable
    // Compare guess and coin flip result
    // Update bank amount for success/failure
    // ask if player wants to play again
}

cout << "Thank you for playing. Your bank balance is " << bank << endl;


How exactly you break out the functions is up to you. But remember that the goal is to have functions that are simple and straightforward whenever possible. It forces you to think of the program as several small, discrete units rather than a globby mess of confusion.
Last edited on
Topic archived. No new replies allowed.