# Simple Guessing Game

• 09-01-2006
Elk
Simple Guessing Game
This is in no way any assignment. I am new and i'm attempting to learn some C.

I was just wondering on some sort of basic idea to make a guessing game. Say you have to pick a number between 1 and 1000 and the number is randomly chosen by the computer (The part I can NOT get). You then input the number into the program and it tells you higher, lower, or if you have won. After winning, the program exits. But untill you have won, the game loops. Just somethin i tried doin after learning some things but i couldn't get the computer to pick a random number and have you guess it. Just wonderin if someone could gimmie a little help there.
• 09-02-2006
Salem

int target = 50;
Then implement all your code to prompt the user for values, and loop until they get the guess right.
This proves the logic, and gets all the user interaction stuff working.

http://faq.cprogramming.com/cgi-bin/...&id=1043284385
and then you can do
int target = genrand(1,1000);
and the jobs a good'un.
• 09-02-2006
Elk
where do i place target = genrand(1, 1000); into the code? Do I use a for loop?
idk it looks like crap but this is all i've got hmmm...

Code:

```#include <stdio.h> #include <stdlib.h> #include <time.h> main() { int close; int repete = 1; int target = 1; int input; while (repete = 1) {                 target = genrand(1, 1000);         printf("Pick a number between 1 and 1000\n");         scanf("%d", &input);         if (input < target)         { printf("Higher than %d\n"), input; }         else if (input > target)         { printf("Lower than %d\n"), input; }                 if (input == target)         { printf("Correct! The correct number was %d!\n"), input;         printf("Press a key then hit enter to exit the program\n");         scanf("%d", &close);         return 0; } } }```
• 09-02-2006
Its really easy program.hare is its code

Code:

```#include<stdio.h> #include<conio.h> #include<stdlib.h> void main() {     int x,g;     randomize();     x=random(1000);     while(1)       {           printf("\n GUESS MY NO::");           scanf("%d",&g);           if(x==g)           {                   printf("YOU WON");                     break;           }           if(g<x)           {                   printf("TOO LOW");             }           else           {                 printf("\n too high");             }   } }```
• 09-02-2006
Salem
> while (repete = 1)
Use == for comparisons, = is for assignment.

> target = genrand(1, 1000);
1. You need to copy the genrand() function from the FAQ
2. You probably want this outside the loop, otherwise the number you're supposed to guess keeps changing.

> printf("Higher than %d\n"), input;
Put the parameter inside the function call, like so
printf("Higher than %d\n", input );

> scanf("%d", &close);
How to pause the program before exiting is also in the FAQ.

The whole point of the board is to help people, not just post alternative answers which don't teach anyone anything. Besides, when people don't use code tags and use void main and a bunch of non-standard headers and functions, it really is not helping at all.
• 09-02-2006
Ken Fitlike
Quote:

Originally Posted by Salem
The whole point of the board is to help people, not just post alternative answers which don't teach anyone anything. Besides, when people don't use code tags and use void main and a bunch of non-standard headers and functions, it really is not helping at all.

Amen.

I added the code tags for you since you're relatively new here but please make sure you use them if you post code in future.
• 09-02-2006
Elk
Can't seem to find the genrand() function on the FAQ. And also, is it also possible to set the RAND_MAX to a number and just use srand and rand() function?
• 09-02-2006
quzah
They call it 'GetRand' in the FAQ link they've provided. RAND_MAX is a standard C macro. You don't set it to anything, it's already a specified value. You can just take the return value from rand and apply some basic math to get a range you like. Search the forum for rand or random and you'll get plenty on the topic.

Quzah.
• 09-02-2006
Elk
Ok ty. finally got it working. is there any way to imporve it?

Code:

```#include <stdio.h> #include <stdlib.h> int GetRand(int min, int max); int main(void) { int i, r, x; for (i = 0; i < 1001; i++) { r = GetRand(1, 1000); } while (1) {         char last;         printf("Pick a number between 1 and 1000\n");         scanf("%d", &x);         if (x < r)         { printf("Higher than %d\n", x); }         if (x > r)         { printf("Lower than %d\n", x); }                 if (x == r)         { printf("Correct! The correct number was %d!\n", r);         scanf("%c", &last);         return 0; } } } int GetRand(int min, int max) {   static int Init = 0;   int rc;     if (Init == 0)   {     srand(time(NULL));     Init = 1;   }     rc = (rand() % (max - min + 1) + min);     return (rc); }```
• 09-03-2006
Salem
> for (i = 0; i < 1001; i++)
This loop serves no purpose, just doing

r = GetRand(1, 1000);
once is sufficient.

Your indentation and brace positioning style could be better.
• 09-03-2006
Driver
Maybe it's a personal thing, but I really hate breaking out of infinite loops. Rather, I like to see a controlled exit using something like this (haven't re-entered the GetRand function):

Code:

```#include <stdio.h> #include <stdlib.h> int GetRand(int min, int max); int main(void) {         int i, r, x;         char last;         r = GetRand(1, 1000);         printf("Pick a number between 1 and 1000\n");         scanf("%d", &x);         while (x != r)         {                 if (x < r)                 {                         printf("Higher than %d\n", x);                 }                 else                 {                         printf("Lower than %d\n", x);                 }                 printf("Pick a number between 1 and 1000\n");                 scanf("%d", &x);         }                 printf("Correct! The correct number was %d!\n", r);         scanf("%c", &last);         return 0; }```
However, as you can see it involved two calls to get the input number. This could very easily be put into a function, though.

Also, I wouldn't declare a variable within a loop, so I moved the declaration of 'last' outside of it.
• 09-03-2006
quzah
Quote:

Originally Posted by Angoid
However, as you can see it involved two calls to get the input number. This could very easily be put into a function, though.

Code:

```do {     printf( "Pick a number: " );     fflush( stdout );     if( scanf( "%d", &number ) != 1 )         printf( "Try again.\n" );     else     if( number < r )         printf( "Higher.\n" );     else     if( number > r )         printf( "Lower.\n" ); } while( number != r );```