I am currently trying to write a C++ program. The intent of the program is to guess what side a coin is going to fall on, heads or tails.
I feel like I am very close to making it work. But I can not seem to figure out how to generate random H & T's and then compare them to the users input.
I am using a past program that I did, where I had the user guess 1's and 0's instead of heads or tails. I bet this is easy to solve and I may be to tired to see it but i hope someone can help me in the right direction.
this is what i have so far...
#include <iostream>
#include <ctime>
using namespace std;
int main ()
{
char guess;
char ans;
int no_of_plays;
const int LOW = 0; //It will auto range the output to within H and T
const int HIGH = 1;
int random_integer;
cout << "THE GAME OF HEAD OR TAILS" << endl;
cout << "" << endl;
cout << "Do you want to play? " << endl;
cout << "Y/y for yes, N/n for no " << endl;
cin >> ans;
if (ans == 'y' || ans == 'Y') {
cout << "How many times do you want to play? " << endl;
cin >> no_of_plays;
}
else {
cout << "Thanks for playing! " << endl;
}
for ( int a = 0; a < no_of_plays; a++)
{
cout << "Call it, Heads or Tails?" << endl;
cout << "h for heads t for tails" << endl;
cin >> guess;
srand((unsigned int) time(NULL));
random_integer = rand() % (HIGH - LOW + 1) + LOW;
if (random_integer == 1)
{
cout << 't'<< endl;
}
if (random_integer == 0){
cout << 'h' << endl;
}
if (guess == random_integer) cout << "You got it!" << endl;
}
system ("pause");
return 0;
}
I know it is probably easy to fix but any help is appreciated!!
BTW I am aware that in the beginning question "do you want to play?" if you type N it still plays anyway...I have fixed it but i did not put it in this code. just incase you are wondering.