#include <stdio.h>
#include <math.h>
#include <conio.h>
#include <process.h>
#include <string.h>


void main()
{
    int i, n, diff;
    int x = rand() % 100;
    int bMoveHigher = 0;
    int bGuessedCorrectly = 0;

    printf("Welcome to Guess a Word Program\n");

    for (i = 1; i <= 5; i++)
    {
          printf("ATTEMPT %d: Enter the your number: ", i);
          scanf("%d", &n);
          if (n == x)
          {
                printf("Congrats! You have guessed the number correctly\n");
                bGuessedCorrectly = 1;
                break;
          }
          diff = (int)(fabs(x - n));
          if(x > n)
                bMoveHigher = 1;
          if(diff >= 50)
          {
                if (bMoveHigher == 0)
                      printf("Your guess is VERY HIGH\n");
                else
                      printf("Your guess is VERY LOW\n");
          }
          else if (diff >= 30)
          {
                if (bMoveHigher == 0)
                      printf("Your guess is HIGH\n");
                else
                      printf("Your guess is LOW\n");
          }
          else if (diff >= 15)
          {
                if (bMoveHigher == 0)
                      printf("Your guess is MODERATELY HIGH\n");
                else
                      printf("Your guess is MODERATELY LOW\n");
          }
          else
          {
                if (bMoveHigher == 0)
                      printf("Your guess is SOMEWHAT HIGH\n");
                else
                      printf("Your guess is SOMEWHAT LOW\n");
          }
    }
    if (bGuessedCorrectly == 0)
    {
          printf("Unfortunately you did not guess it correctly. The correct number is: %d\n", x);
    }


}

Inline Code Example Here

Do you have a question?

the program doesn't work.. it always displays rand should have a prototype..

can you please help me identify the error..

You have to include stdlib.h

The short answer is, rand() and srand() (which you should call at the beginning of the program before calling rand() for the time) are in <stdlib.h>, so you need to #include that header in order to get the function prototypes for them.

That having been said, I will add that you don't use any functions from either <conio.h> (which you shouldn't be using anyway) or <process.h> (which is specific to Unix-like systems anyway). Oh, and main() should always be declared int, not void.

On a guess, I figure you are using Turbo C++ 1.01, right? DON'T. It is a twenty-five year old MS-DOS compiler that won't run on modern Windows systems without an emulator such as DosBox, and pre-dates the current C and C++ standards. If your professor is requiring you to use it, walk away from the class and don't look back, because you are being taught things that are going to make it harder for you to learn the right ways to program in the modern world. Try to get as many other students to walk out with you - the only way to get them to change their absurd and outdated policies is to throw them back in their faces. If you have to, learn programming on your own - chances are, you'll do better that way than you would trying to make that class work out for you.

commented: nice info about Turbo C +14
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.