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.

1)I have to include

#include <cstdlib> // for srand

to define the srand() symbol.

2) There is no reason for all of this:

random_integer = rand() % (HIGH - LOW + 1) + LOW;

Since you just want a number from 0 to 1, you can just use rand() 3)Your main problem is that you are trying to compare 'h' or 't' to 0 or 1! This will not work!

Below is how I would do it. I just set a char coin variable based on the int variable that you generate. Then I compare the char guess with the char coin.

#include <iostream>
#include <ctime>
#include <cstdlib> // for srand
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;
        char randomCoin;
        if (random_integer == 1)
         {
           cout << "coin was tails" << endl;
           randomCoin = 't';
         }
        if (random_integer == 0)
        {
         cout <<  "Coin was heads" << endl;
         randomCoin = 'h';
        }
        if (guess == randomCoin)
        {
          cout << "You got it!" << endl;
        }
        else
        {
          cout << "You missed it!" << endl;
        }
     }
     system ("pause");
     return 0;
     }

Good luck,

David

Thank you! you even made it better then what I originally planning to do!! thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.