jeudi 1 août 2019

What can i do to make integer random everytime

I created a little guessing game, where you need to guess a number in 3 tries or you will fail. Now question is what can i do to make integer random every time i run program.

Here is a code:

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

int main()
{
    int sN = 4;
    int g;
    int gC = 0;
    int gL = 3;
    int ofG = 0;

    while(g != sN && ofG == 0)
    {
        if(gC < gL)
        {
            printf("Guess a number[1-10]: ");
            scanf("%d", &g);
            gC++;
        }
        else
        {
            ofG = 1;
        }
    }
    if(ofG == 1)
    {
        printf("You Failed! [Out of guesses]\n");
        printf("Secret number was (%d)\n", sN);
    }
    else
    {
        printf("Congratulations! You win!");
    }

  return 0;
}

Show me how i can do it and explain what you did and what command/function does if you can because i'm new and i don't understand things very well. I guess i need to use

srand();

but i never used it before so any help would be appreciated.




Aucun commentaire:

Enregistrer un commentaire