hi everyone am trying to write a very small interactive program that 'thinks' of a number in the range of 0 to 99 and asks the user to guess it , problem is when i compile it (using devc++) I get errors (six to be exact) I don't know what is going on , below is the code , i know there is always someone out there to help :cheesy: thanx p.s please explain what the rand() function exactly is, thanx again

#include <stdio.h>

main()
 {
   int target;
   int guess;
   int again;

   printf("n Do you want to guess a number 1 =Yes, 0=No ");
   scanf("%d",&again);

   while (again)
    {
      target = rand() % 100;
}      guess  = target + l;

      while(target!=guess)
       {
         printf('n What is your guess ? ");
         scanf("%d",&guess);

         if (target>guess) printf("Too low");
         else printf("Too high");
       }

      printf("n Well done you got it! n");
      printf("n Do you want to guess a number 1=Yes, 0=No");
      scanf("%d".&again);

      system("PAUSE");
      return 0;
    }
}

Dani AI

Generated

Good question, . A few grounded notes to complement the thread: rand() is a C standard library pseudo-random number generator that returns an int in the range [0, RAND_MAX]. Include stdlib.h (or cstdlib in C++) and seed the generator once with srand() so you do not get the same sequence every run. Seeding typically uses the current time, which comes from time.h (or ctime). is right that it is not in math.h, and is right to point out that warnings matter; they often reveal real bugs.

Common fixes for your program based on the replies: use int main(void), fix the stray quotes, make sure you meant 1 not l, and move braces so the guessing loop and the prompt to set again are inside the outer loop. Also, check the result of scanf and avoid system("PAUSE") if you can.

Minimal C skeleton showing proper seeding and target generation:

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

int main(void) {
    srand((unsigned)time(NULL));      /* seed once */
    for (int again = 1; again; ) {
        int target = rand() % 100;    /* 0..99 (roughly uniform) */
        /* read guesses, print hints, and set 'again' here */
        /* ... */
    }
    return 0;
}

If you want an actually uniform distribution without modulo bias (and better quality RNG), prefer modern C++ as this forum tag suggests. Example:

#include <random>
std::mt19937 rng(std::random_device{}());
std::uniform_int_distribution<int> dist(0, 99);
int target = dist(rng);

Quick tips:

  • Always validate input: if scanf("%d", &guess) != 1, clear the input and reprompt.
  • Print a newline after messages so output flushes predictably.
  • Seed once before the game loop; do not call srand inside the loop.

Recommended Answers

All 8 Replies

You need to include <math.h> to use the rand() function, and since "again" is never changed during the while loop, it will repeat that until you stop the program, so I think you'd need to move the closing bracket to the line before system("PAUSE").

purpleposeidon you wouldn't belive what caused the error , :o a simple syntax error !!1 :o i put a single quote instead of two after the printf(' , well you live to learn from your mistakes i guess :cheesy: thanks for the help , i really appreciate it .next time i'll check twice before i post my questions :o

Hello,

This is the output of the compiler (gcc 3.3.5) (with my comments inside):

rand.c:4: warning: return type defaults to `int'
/* the main() function should be declared as int main() */
rand.c: In function `main':
rand.c:14: warning: implicit declaration of function `rand'
/* rand() is defined through the header math.h, as purpleposeidon said */
rand.c:15: error: `l' undeclared (first use in this function)
/*probably you meant '1 (one)', not 'l' */
rand.c:15: error: (Each undeclared identifier is reported only once
rand.c:15: error: for each function it appears in.)
rand.c:19:8: missing terminating ' character
rand.c:19:8: warning: character constant too long for its type
/* printf thinks that's a single character with ' ' */
rand.c:20: error: parse error before "scanf"
/* related to previous error (missing ") */
rand.c:28: error: parse error before '&' token
/* replace . with , */
rand.c:30: warning: implicit declaration of function `system'
/* system() requires stdlib.h */
rand.c: At top level:
rand.c:33: error: parse error before '}' token
/* superfluous '}' */

The warnings don't prevent compilation but errors do!
Dont' forget to indent properly!

Ari

>/* rand() is defined through the header math.h, as purpleposeidon said */
stdlib.h, not math.h.

>The warnings don't prevent compilation but errors do!
The warnings should be addressed though. They're there for a reason.

thanx airflipie ,narue , you guys are helpful like always :cheesy: yes and i checked my book again , yes rand() is indeed defined the header file stdlib.h

>/* rand() is defined through the header math.h, as purpleposeidon said */
stdlib.h, not math.h.

Of course, it's stdlib.h :o .

The warnings should be addressed though. They're there for a reason.

That's why I mencioned the errors and warnings. The ouput came from

$ gcc -Wall -o rand rand.c

Warnings are also welcome :cool:.

Ari

>$ gcc -Wall -o rand rand.c
How about:

$ gcc -W -Wall -ansi -pedantic -o rand rand.c

:D

>$ gcc -Wall -o rand rand.c
How about:

$ gcc -W -Wall -ansi -pedantic -o rand rand.c

:D

You're the guru :)

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.