Write your question here.
I need help on how I would go about coding this problem. My assignment is to write a program that lets the user guess whether the flip of a coin results in heads or tails. The program randomly generates an integer 0 or 1, which represents head or tail. The program prompts the user to enter a guess and reports whether the guess is correct or incorrect.
The code below is a code that may help because its a random number guessing game.
int main()
{
srand(time(0));
int number = rand() % 101;
int guess = 0;
cout << "Guess a magic number between o and 100";
while (guess != number)
{
cout << "\nEnter your guess: ";
cin >> guess;
if (guess == number)
cout << "Yes, the number is\t" << number << endl;
elseif (guess > number)
cout << "Your number is too high" << endl;
else
cout << "Your number is too low" << endl;
}
return 0;
}
The program randomly generates an integer 0 or 1, which represents head or tail.
int number = rand() % 101;
==> int number = rand() % 2; // You only need '0' and '1'
The program prompts the user to enter a guess and reports whether the guess is correct or incorrect.
1 2 3 4 5 6
if (guess == number)
cout << "Yes, the number is\t" << number << endl;
elseif (guess > number)
cout << "Your number is too high" << endl;
else
cout << "Your number is too low" << endl;
There is no higher number and lowest number. The only two options here are 'correct' and 'incorrect'. Make use of this hint to complete the rest of the assignment.
Thanks for the help and response. I was able to get the game working. However, my while loop is not working. The code runs, but it infinitely displays the answer instead of just once.