Hello,
I am working on some C++ exercises. I'm a beginner and having a lot of trouble moving forward with this one:
"Create a coin-flipping game. Ask the user how many times to flip the coin, and use the random function to determine heads or tails each time a coin is flipped.
Assume the user starts with $50. Every time the coin is flipped calculate their winnings (win +$10, lose -$10). Create another function to test if the user has gone broke yet (THIS FUNCTION MUST RETURN A BOOLEAN TRUE/FALSE VALUE). End the program either when they’ve gone broke or when the coin was flipped the number of times the user had specified.
Display 1) how many times the coin was flipped, 2) the number of times “heads” was the result, 3) the number of times “Tails” was the result, and 4) how much money they’ve won/lost."
So far, this is the code I've written:
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
|
#include <iostream>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
void displayFlip(int z)
{
cout << "Times flipped:" << endl;
cout << z << endl;
}
int main()
{
cout << "How many times would you like to flip the coin?" << endl;
int x;
int z;
cin >> z;
displayFlip(z);
cout << "Result:" << endl;
srand(time(0));
for (int x = 0; x < z; x++)
cout << 1 + (rand() % 2) << endl;
cout << "Heads returned:" << endl;
cout << "Tails returned:" << endl;
}
|
Please feel free to edit or completely take out some parts. I'm trying to figure out how to work with random numbers--and making games like these. Please stick within the headers I listed though. Thank you for your replies!