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;
    }
}

Edited 3 Years Ago by pyTony: fixed formating

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

You're the guru :)

This article has been dead for over six months. Start a new discussion instead.