I\'m having trouble with this homework problem. I\'ve found an answer similar to
ID: 3712180 • Letter: I
Question
I'm having trouble with this homework problem. I've found an answer similar to what im looking for but I need it in strictly c++ and not c or any other derivative. Also the instructions have to be followed exactly with norhing left out
Write a program that is a guessing game.
1. The user must start with 0 points
2. The computer will “roll” a digital six-sided die and “flip” a digital two-sided coin
3. The user must guess the number on the die (1,2,3,4,5, or 6) and the side the coin has landed on (H for heads, T for tails)
4. The roll and flip result revealed
5. If the user was right then they win 10 new points. If the user was wrong, then they lose one point
6. The user is asked whether to continue the game, if so then the program goes back to step two
——————
In this program you need to define at least the following functions:
1. RollAndflip(...) - called by main() ~ it computes the output of a random die roll and coin flip
2. All user input must be obtained through userInput() function
3. You must use the srand( ) and the rand( ) functions and make sure all three functions are call-by-reference
—————
A sample output is :
./guessinggame.out
Guess a (6-sided) die roll and a coin-flip: 3 H
Incorrect! Die roll was 5, coin flip was H.
Total points: -1
Do you want to try again? (Y/N). Y
Guess a (6-sided) die roll and coin flip: 1 H
Incorrect! Die roll was 2, coin flip was T.
Total points: -2
Do you want to try again? (Y/N) Y
Guess a (6-sided) die roll and a coin flip: 5 T
Correct! Die roll was 5, coin flip was T.
Total points : 8
Do you want to try again? ( Y/N) N
Goodbye! Thanks for playing!
Explanation / Answer
#include<iostream>
#include<string>
#include<cstdlib>
#include<time.h>
using namespace std;
void userInput(int d, char a, int &p){
int n;
string flip;
cout << "Guess a (6-sided) die roll and a coin flip:";
cin >> n >> flip;
if (d == n && flip[0] == a)
p = p + 10;
}
void RollAndFlip(int &d, char &c){
d = 1 + rand()%6;
if (rand()%2 == 0)
c = 'H';
else
c = 'T';
}
int main(){
int points = 0;
string ch;
int dice;
char flip;
srand(time(NULL));
while(true){
RollAndFlip(dice, flip);
userInput(dice,flip,points);
cout << "Total Points : " << points << endl;
cout << "Do you want to try again? (Y/N) ";
cin >> ch;
if (ch[0] == 'N')
break;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.