0

I have started C recently and am having trouble make the computer think of a random number. This is the code so far. I need help!

#include <stdio.h>
#include <stdlib.h>
int main ()
{
    time_t t;
    int userin;
    printf("Guess a number from 1 to 10\n");
    scanf("%d", userin);
    int r = rand() % 11;

    if (r == userin)
    {
        printf ("you are right");
    }
    else
    {
        printf("Try again");
    }
    return 0;
}

Thx a lot guys it worked out!!!!

  • 2
    You don't call `srand` function. Include `time.h` and call `srand(time(NULL));` after variables declaration. – Misaz Jul 06 '16 at 18:47
  • 2
    Missing `&` while reading: `scanf("%d", &userin);` instead of `scanf("%d", userin);` – Rajat Jul 06 '16 at 18:51
  • Interesting that code generates the random number after user input. Of course the order is not that important - it just seems like the code is cheating with knowing user input first. Could simply code `puts("Guess a number from 1 to 10"); scanf("*%s"); puts(rand()%10 == 0 ? "you are right" : "Try again");` Is a compare to proper user input really needed? User win 1/10 of the time in either case. – chux - Reinstate Monica Jul 06 '16 at 21:04

5 Answers5

5

In your code, r will be a random number from 0 to 10. For a random number between 1 and 10, do this:

int r = rand() % 10 + 1;

Also, you should call

srand(time(NULL)); 

at the beginning of main to seed the random number generator. If you don't seed the generator, it always generates the same sequence.

user3386109
  • 34,287
  • 7
  • 49
  • 68
2

There is issue in your scanf statement as well.

You should use

scanf("%d", &userin); 

instead of

scanf("%d", userin); /* wrong - you need to use &userin */

scanf needs the address of variables at which it will store the value. For a variable, this is given by the prefexing the variable with &, as in &userin.

sps
  • 2,720
  • 2
  • 19
  • 38
2

There are few issues in your code.

  1. not reading into the address & of your variable using scanf
  2. not considering "legitimate" values of input, result of rand()%11 can also be 0
  3. not checking against "illegal" input values, which can "alias" the result.
  4. not properly initializing seed of the pseudo-random rand() function, so it always returns the same result.

Using printf for debugging your code, as in the following example, based on your code can help a lot:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DEBG 1

int main (void)
{
    time_t t;
    int userin;
    printf("Guess a number from 1 to 10\n");
    if(scanf("%d", &userin) != 1){  // read into the variable's address
        printf("Conversion failure or EOF\n");
        return 1;
    }

    if(userin < 1 || userin > 10){  // check against "illegal" input
        printf("Offscale, try again\n");
        return 1;
    }

    srand(time(NULL));              // initialize the seed value
    int r = 1 + rand() % 10;        // revise the formula

    if (DEBG) printf("%d\t%d\t", r, userin);  //debug print

    if (r==userin){
        printf ("you are right\n");
    }else{
        printf("Try again\n");
    }
    return 0;
}

Please, also consult this SO post.

Community
  • 1
  • 1
user3078414
  • 1,942
  • 2
  • 16
  • 24
  • Thanks for your suggestion, @chux, really a good programming habit: one shouldn't ignore `scanf()` return value. (-: – user3078414 Jul 07 '16 at 07:43
0

Problems :

scanf("%d", userin); //you are sending variable 
  • This is not right as you need to send address of the variable as argument to the scanf() not the variable

so instead change it to :

scanf("%d", &userin); //ypu need to send the address instead

and rand()%11 would produce any number from 0 to 10 but not from 1 to 10

as other answer suggests, use :

(rand()%10)+1 //to produce number from 1 to 10 

Solution :

And also include time.h function to use srand(time(NULL));

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

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

    int userin;

    printf("Guess a number from 1 to 10\n");
    scanf("%d", &userin);
    int r = (rand() % 10)+1;

    if (r==userin)
    {
        printf ("you are right");
    }
    else
    {
        printf("Try again");
    }

    return 0;
}

Why use srand(time(NULL)) ?

  • rand() isn't random at all, it's just a function which produces a sequence of numbers which are superficially random and repeat themselves over a period.

  • The only thing you can change is the seed, which changes your start position in the sequence.

and, srand(time(NULL)) is used for this very reason

Community
  • 1
  • 1
Cherubim
  • 5,287
  • 3
  • 20
  • 37
0

This should work

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

    int main ()
  {
        int userIn = 0; //I like to initialize
        printf("Guess a number from 1 to 10\n");
        scanf("%d", &userIn);

        srand(time(NULL)); //seed your randum number with # of seconds since the Linux Epoch

        int r = (rand()%10)+1; //rand%11 gives values 0-10 not 1-10.  rand%10 gives 0-9, +1 makes sure it's 1-10

        if (r == userIn)
        {
        printf ("you are right\n");
        }
        else
        {
        printf("Try again\n");
        }
        return 0;
   }

Edit: You may want to implement code to verify that the user input is in fact an integer.

MacedonZero
  • 216
  • 1
  • 9