# Thread: Random number guessing game

1. ## Random number guessing game

I'm on my way to make a Hang Man game so I thought I'd develop a random number guessing game first.

Here's the code:

Code:
```#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main() {
int RandomNumber, Guess;
time_t RandomSeeder;

srand((unsigned) time(&RandomSeeder));

RandomNumber = rand() % 100;

scanf("%d", &Guess);

while(Guess > RandomNumber || Guess < RandomNumber) {
if(Guess > RandomNumber)
printf("Too high!\n");

if(Guess < RandomNumber)
printf("Too low!\n");

printf("Try again: ");

scanf("%d", &Guess);
}

if(Guess == RandomNumber)
printf("You win!\n");

return 0;
}```
I know I'm using scanf but this was a quick throw together.

I've forgotten how to use other methods to make sure a user inputs a number instead of a character. I'll be looking into that in a bit...so yes, there's a feature where if you type a few characters, it'll go into an infinite loop. 2. my two cents
Code:
```#include <stdio.h>
#include <time.h>
#include <stdlib.h>

int main() {
int RandomNumber, Guess;
time_t RandomSeeder;

srand((unsigned) time(&RandomSeeder));

// RandomNumber = rand() % 100;
RandomNumber = 50;

scanf("%d", &Guess);

while(Guess > RandomNumber || Guess < RandomNumber)
{
if(Guess > RandomNumber)
printf("Too high!\n");
else // it's either one or the other yes?
// if(Guess < RandomNumber)
printf("Too low!\n");

printf("Try again: ");

// gets the value here so it does not go though the if statements frist
// but the conditional check first. so if match it kicks it out of loop.
// so no real need to worry about if statements for everything. if else suffices.
// if you'd put that scanf on top before the if stameants then that'd be a different story.

scanf("%d", &Guess);
}

if(Guess == RandomNumber)
printf("You win!\n");

return 0;
}```
just gave it a hard coded value to check it.
Code:
```\$ ./term2
Too low!
Try again: 51
Too high!
Try again: 50
You win!``` 3. I've done a bit more work. This time I can accept characters (except for the looper variable, haven't done that yet). My goal is to just reject anything that's not a number. I'll get there.

Here's the new code:

Code:
```#include <stdio.h>#include <time.h>
#include <stdlib.h>

int Guess;
char UserGuess;

void NumberInput(void);

int main() {
int looper;
looper = 1;

while(looper) {
int RandomNumber;
time_t RandomSeeder;

srand((unsigned) time(&RandomSeeder));

RandomNumber = rand() % 100;

printf("Number guess: ");

NumberInput();

while(Guess > RandomNumber || Guess < RandomNumber) {
if(Guess > RandomNumber)
printf("Too high!\n");

else
printf("Too low!\n");

printf("Try again: ");

NumberInput();
}

if(Guess == RandomNumber)
printf("You win!\n");

printf("Play another game?\n");
printf("1. yes, 2. no\n");

scanf("%d", &looper);

if(looper == 2)
looper = 0;
}
return 0;
}

void NumberInput() {
while(1) {
fgets(UserGuess, 64, stdin);

Guess = atoi(UserGuess);

if(Guess > -1 && Guess < 101)
break;

else
printf("Enter a number between 0 to 100\n");
}
return;
}``` 4. Wait.. making adjustments to a function I wrote awhile back to see if I can get it to completely work in here. just cuz o tay look at this,
Code:
```void NumberInput() {
while(1) {
fgets(UserGuess,sizeof UserGuess, stdin);

// Guess = atoi(UserGuess);
if ( ( Guess = int_or_ch(UserGuess) ) == -1)
{
printf("enter a number silly %d\n"
"Try again ", Guess);

}
else if(Guess > -1 && Guess < 101)
break;
else
printf("Enter a number between 0 to 100\n");
}
return;
}```
output
Code:
```\$ ./term2
Number guess: r
enter a number silly -1
Try again r
enter a number silly -1
Try again r
enter a number silly -1
Try again r
enter a number silly -1
Try again 4
Too low!
Try again: e
enter a number silly -1
Try again```
no I didn't post the function, did want to deprive you of your fun. But if you want some hints. post for some. 5. Are you able to tell me some of the functions used in your function without giving away how it works? 6. is uses isdigit and a char if more than 1 , like 20 = 2 then char is looked at as an array within isdigit. to get it back to 1 to see char as an int. it is a stinky tricky thing to try and get to work. I still have issues with it. I know me and whiteflags fiddled with this issue about how to get input to see if it is a digit or not a while back, It might be in C++ side. if not here, ok found it, YOU DO NOT HAVE TO CLICK ON IT, RESIST THE TEMPTATION Mhumhahahahahaha

help with a function that determins digit or not digit needed, apply within. Popular pages Recent additions game, guess, number, random, randomnumber 