mercredi 28 mars 2018

How do I implement my randomly generated word into a condition? - C

I am creating simple heads or tails game, however, I am trying to come up with a condition where if you insert "h" and the randomly generated answer is heads, the code prints that you get the answer correct. The randomly generated heads or tails is working fine, however, I do not know how to refer to the result of the flip, in the if condition at the end of my code.

Here's my code so far:

#include<stdio.h>
#include<unistd.h> //for sleep function
#include <stdlib.h> //for array
#include <time.h> //for random array

    //Array 
  int cool(void) {
    int i;
    int ht;

    i = 2;
    ht = 0;
    srand((unsigned)time(NULL));
    ht = rand() % i;
    return ht;
}

int main(int argc, char **argv)

And the second part

const char* o[2] = { 
     "Heads", "Tails"       //Where the words heads and tails are stored 
     };

//Coin Flip begins
    printf("\nType 'h' for heads & 't' for tails\n");
    printf("\nHeads or Tails? \n");
    scanf("%c", &a);
    //sleep(3);
    printf("%s\n",o[cool()]); //random heads or tails generated
    //sleep(3);


    if ( o == "Heads" && a == 'h' || o== "Heads" a == 'H' )
    {
        printf("\nCorrect!\n");

    }

    return 0;

}

Apologies if this has been asked before or I have asked incorrectly. I am new to coding and stack so feedback would be helpful




Aucun commentaire:

Enregistrer un commentaire