mercredi 8 novembre 2017

how to write guessing game in C?

We're supposed to write a program that allows a user to play a guessing game. Pick a random number in between 1 and 100, and then prompt the user for a guess. For their first guess, if it’s not correct, respond to the user that their guess was “hot.” For all subsequent guesses, respond that the user was “hot” if their new guess is strictly closer to the secret number than their old guess and respond with “cold”, otherwise. Continue getting guesses from the user until the secret number has been picked.

What i have so far:

#include<stdio.h>
#include<stdlib.h>


int main()
{
    srand(time(NULL));

    int guess1;
    int guess2;

    int randNum = rand() % 100 + 1;
    printf("Random number is %d\n", randNum);

    int diffRandGuess1;
    int diffRandGuess2;


    printf("Please guess a whole number in between 1 - 100: ");
    scanf("%d", &guess1);

    if (guess1 < 1 || guess1 > 100)
    {
        printf("Failed to enter proper number. Program ended.\n");
    }
    else if (guess1 != randNum)
    {
        printf("Hot.\n");
        printf("Please enter another number: ");
        scanf("%d\n", guess2);

        diffRandGuess1 = randNum - guess1;
        diffRandGuess2 = randNum - guess2;

        while(diffRandGuess1 < diffRandGuess2)
        {
            printf("Hotter.\n");
            printf("Please enter another number: ");
            scanf("%d\n", guess2);


            if (guess2 = randNum)
            {
                printf("You finally got it!!!");
            }

        }
    }

    else if (guess1 = randNum)
    {
        printf("Damn you got it on the first try!\n");
    }






return 0;
}




Aucun commentaire:

Enregistrer un commentaire