Question

In: Computer Science

C++ Create a program that simulates a coin being flipped. Ask the user to guess between...

C++

Create a program that simulates a coin being flipped. Ask the user to guess between heads and tails. Let the user input 0 for heads and 1 for tails.

Use a random generator to get random guesses every time.

If the user guesses correctly, give them 1pt. Use a counter and initialize it to 0.  

If the user does not guess correctly, subtract a point.

Create a menu that allows the user to continue guessing, view the current score or quit guessing.

Note: You can use classes, pointers... Do not go beyond that. Keep code simple.

Solutions

Expert Solution

include<iostream>
using namespace std;
int coinToss(void) {
    int flip;
    flip = rand()%2  ;
    
    return flip;
}
int main()
{
int flip,Count=0,errorCount=0,guess,score=0,choice;

while(1)
{
cout<< "press 1 to continue guessing: "<< endl;
cout<< "press 2 to view the current score: "<< endl;
cout<< "press 3 to quit guessing: "<< endl;
cout<< "enter your choice:"<< endl;
cin>> choice;
switch(choice) {
case 1:
cout<<" enter 0 to guess head or enter 1 to guess tail ";
cin >> guess;

flip = coinToss();
if(flip == 0)
cout<< "the result is head" << endl;
else
cout<< "the result is tail"<< endl;
if(flip == guess)
{
    cout<< " your guess is correct" << endl;
Count += 1;
} else {
    cout<< " your guess is wrong" << endl;
errorCount += 1;
}
break;

case 2:
score = score + (Count *1) -(errorCount *1);
cout<< "your current score is " << score << endl;
break;
case 3:
cout<< "Thank You for your participation" << endl;
exit(0);
}
}
return 0;
}

Sample Output :


Related Solutions

In C++  Write a program that simulates coin tossing. For each toss of the coin the program...
In C++  Write a program that simulates coin tossing. For each toss of the coin the program should print heads or tails. Let the program toss the coin 100 times and count the number times each side of the coin appears. Print the results. 0 represents tails and 1 for heads.
Create a C++ program that will ask the user for how many test scores will be...
Create a C++ program that will ask the user for how many test scores will be entered. Setup a while loop with this loop iteration parameter. (no fstream) The data needs to include the student’s first name, student number test score the fields should be displayed with a total width of 15. The prompt should be printed with a header in the file explaining what each is: ex. First Name student number Test Score 1) mike 6456464   98 2) phill...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will first enter a password, then enters the same password again; If the second input is the same as first one, the user successfully creates the password. Print “Well done.”; Otherwise, the user will be directed to repeat the whole process (go to step 1.)
Write a C program that asks the user to guess a number between 1 and 15(1...
Write a C program that asks the user to guess a number between 1 and 15(1 and 15 are included). The user is given three trials. This is what I have so far. /* Nick Chioma COP 3223 - HW_2 */ #include <iostream> #include <time.h> // needed for time function #include <stdlib.h> // needed for srand, rand functions int main () {       int numbertoguess, num, correct, attempts;       srand(time(NULL)); //this initializes the random seed, making a new...
Using (C programming language) Create a health monitoring program, that will ask user for their name,...
Using (C programming language) Create a health monitoring program, that will ask user for their name, age, gender, weight, height and other health related questions like blood pressure and etc. Based on the provided information, program will tell user BMI, blood pressure numbers if they fall in healthy range or not and etc. Suggestions can be made as what should be calorie intake per day and the amount of exercise based on user input data. User should be able to...
Q1) Please create a C++ program that will ask the user for how many test scores...
Q1) Please create a C++ program that will ask the user for how many test scores will be entered. Setup a while loop with this loop iteration parameter. The data will include the student’s first name, Wayne State Access ID, midterm score and their favorite team (i.e. – Doug, hg1702, 92, Wolverines * - John, hv2201, 99, Warriors). Print out the completed test scores to a file (midTermScores.txt) . Adjust the output as follows: (15 points) a. Scores with the...
Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i...
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i used random.randint(1,1000) -must give hints like (pick a lower/higher number) which i already did -tell the user when he has repeated a number (help) -the game only ends when you guess the number (help) -you loose $50 every failed attempt (done) ****I did it as a while loop -
Write an application that simulates coin tossing. Let the program toss a coin each time the...
Write an application that simulates coin tossing. Let the program toss a coin each time the user chooses the “Toss Coin” menu option. Count the number times each side of the coin appears. Display the results. The program should call a method flip( ) that takes no arguments and returns a zero to represent a tail or a one to represent a head. There is a Random class that will allow you to generate a random integer. Import it from...
Write a program in c++ that prompts the user to input a coin collection of number...
Write a program in c++ that prompts the user to input a coin collection of number of quarters, dimes, nickels and pennies. The program should then convert the coin collection into currency value as dollars. The coin values should all be whole numbers and the resulting currency value should be displayed with two decimals. An example of user interaction is as follows: Coin Convertor Enter number of quarters: 3 Enter number of dimes: 1 Enter number of nickels: 4 Enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT