Random Heads or Tails

Do I meet the requirements for this assignment? If not, how can I improve the code or make it better? Please help. Here are the instructions:

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.

Here is my code:

#include <iostream>
#include <ctime>

using namespace std;

int main()
{
char choice;

cout << "Welcome to the coin flip game. It costs a dollar to play." << endl;
cout << "If you guess correctly, you will win $2.00" << endl;
cout << "If you guess correctly, you will match your bet one to one." << endl;
cout << "Do you want to play (Y/N)? ";
cin >> choice;

char computer_choice[] {'H', 'T'};

srand (time_t(NULL));

char guess;
int num = rand() % 2;

double bank = 14.00;
int winnings = 0.0;

while (toupper(choice) == 'Y')
{
cout << "Your bank balance is $" << bank << endl;

cout << "Enter heads or tails (H/T) ";
cin >> guess;
guess = toupper(guess);
num = rand() % 2;

char computer_throw = computer_choice[num];
cout << "Computer throws " << computer_throw << '\n';

if (guess == computer_throw)
{
cout << "You win\n";
winnings = 2.00;
}
else
{
cout << "You lost\n";
winnings = -2.00;
}

num = rand() % 2;

bank += winnings;
}

return 0;
}





1
2
    cout << "If you guess correctly, you will win $2.00" << endl;
    cout << "If you guess correctly, you will match your bet one to one." << endl;

What if you guess incorrectly? I think you want the second line to say "If you guess incorrectly, you will lose your bet."
1
2
3
4
5
6
7
        if (guess == computer_throw) {
            cout << "You win\n";
            winnings = 2.00;
        } else {
            cout << "You lost\n";
            winnings = -2.00;
        }

Losing costs you your $1 bet, not $2.

Write a program that starts a player off with a bank of $15.00

Your program starts the player with $14.

You ask if they want to play once, at the beginning of the program. The first bet should happen automatically and then you ask if they want to play again.

You have num = rand() % 2 in three different places. You only need it once.

You should be using plenty of functions [...] unsigned variables ....

I see no functions or unsigned variables.

Here are some ideas for functions:
intro() - displays the introductory text.
getGuess() - prompt the user for their guess. Read it and convert to upper case. Return 'H' or 'T'
playAgain() - ask user if they want to play again and read response. Return true or false, depending on whether they want to play again.
playOnce() - Flip the coin, Get the guess, and display the results. Returns the winnings.

With these functions, the man function becomes:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int
main()
{
    double bank = 15.00;
    bool again = true;

    srand(time_t(NULL));

    intro();
    while (again && bank >= 1.00) {
        cout << "Your bank balance is $" << bank << endl;
        bank += playOnce();
        again = playAgain();
    }
    return 0;
}


You handle only whole dollars, don't you? In other words, you don't need double; an integer should do.

Output could use:
cout << "Your bank balance is $" << bank << ".00\n";
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
#include <iostream>
#include <ctime>
playOnce;
playAgain;

using namespace std;

int main()
{
    char choice;
    
    cout << "Welcome to the coin flip game. It costs a dollar to play.\n" << endl;
    cout << "If you guess incorrectly, you will lose your bet.\n" << endl;
    cout << "If you guess correctly, you will win your bet one to one. \n" << endl;
    cout << "Do you want to play (Y/N)? ";
    cin >> choice;
    
        double bank = 15.00;
        bool again = true;
        
        srand(time_t(NULL));
    
        while (again && bank >= 1.00)
        {
            cout << "Your bank balance is $" << bank << endl;
            bank += playOnce();
            again = playAgain();
        }
        return 0;
    }

    char computer_choice[] {'H', 'T'};

    char guess;
    int num = rand() % 1;
    cout << "Guess heads or tails and I will tell you if you guessed correctly.(H/T)" << endl;
    
    double bank = 14.00;
    int winnings = 0.0;
    
    while (toupper(choice) == 'Y')

    {
        cout << "Your bank balance is $" << bank << endl;
        
        cout << "Enter heads or tails (H/T) ";
        cin >> guess;
        guess = toupper(guess);
        num = rand() % 1;
        
        char computer_throw = computer_choice[num];
        cout << "Computer throws " << computer_throw << '\n';
        
        if (guess == computer_throw)
        {
            cout << "You win\n";
            winnings = 2.00;
        }
        else
        {
            cout << "You lost\n";
            winnings = -2.00;
        }
        
        cout << "The coin landed on Tails\n" << endl;
        
        cout << "YOU WIN $2.00" << endl;
    
        cout << "Would you like to play again? (Y/N)" << endl;
        
        bank += winnings;
        cout << "Thank you for playing. Your bank balance is $16.00." << endl;
    }
    
    return 0;
}


These were the changes I made but I get an error on lines 7,8,40.45,79, and 80. Why is that? And is the code better? If not how can I fix it?
I get an error on lines 7,8,40.45,79, and 80. Why is that?

It looks like you just copied and pasted the body of my main() program into yours without writing the other functions. Reread my post. You need to move sections of the main() program into the functions that I suggested (and make a few changes along the way).
The lines 7,8, do they give error messages like these:
1
2
error: 'playOnce' does not name a type
error: 'playAgain' does not name a type 


Those errors match to these lines:
1
2
playOnce;
playAgain;

that are in global scope.

Are these attempts to declare functions? They need a bit more.
http://www.cplusplus.com/doc/tutorial/functions/
http://en.cppreference.com/w/cpp/language/function


Overall, the error messages do contain useful information. Pay attention to them.
You realize that you are seeding with the same value every time? You should follow the examples in the documentation to seed the RNG.

srand( time( NULL ) );

The lowest bit of the randomly-generated number is not a good one to switch on. Choose a higher bit, like bit 5.

num = (rand() >> 4) & 1;

Alas, I don't have time or resources right now to look deeper into your code. Hope this helps a little.
It does thank 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

#include <iostream>
#include <string>
#include <ctime>
#include <cmath>

using namespace std;

char 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;
    
    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;
    
    
    
    
    char 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;
}

char  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;
}

I kind of fixed it, but I keep getting errors on lines 86,88, and 94 :(. Does this code look better than before? Or am I way off...
One thing I noticed is that even if you enter 'N', you're not forced to play but you see some of the output and prompts you shouldn't be getting if they don't want to play. Another thing, your while loop is doing absolutely nothing.

Also that char CoinFlip(){ /* */ } function, it's returning a String type and the function is of type Char

1
2
main.cpp:94:16: error: invalid conversion from 'const char*' to 'char' [-fpermissive]
         return "Heads";


@Duthomhas
Alas, I don't have time or resources right now to look deeper into your code. Hope this helps a little.


:oooooo its 2018 man cloud resources galore.
shell.cpp
https://www.onlinegdb.com/
Last edited on
Topic archived. No new replies allowed.