In: Computer Science
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.
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 :