Reputation: 1
I am trying to make a number guessing game in C in which the computer picks a number and you have to guess which number it is. It states too high if the number you picked is greater than the number to guess and vice versa. However, in my implementation, no matter what number you guess, it is incorrect. I would appreciate if you could tell me what is wrong with the code below.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void){
srand(time(NULL));
char instring[1];
int inint;
int guess;
guess=rand();
while (guess!=inint){
printf("Guess Number\r\n");
gets(instring);
inint=atoi(instring);
if(inint>guess){
puts("too high");
}else if(guess>inint){
puts("too low");
}else{
puts("right");
}
}
return 0;
}
Upvotes: 0
Views: 360
Reputation: 21
Your correct guessing is inside of while (guess != init)
.
That's the reason you will have always an incorrect guessing.
Upvotes: 0
Reputation: 29266
char instring[1];
A C string needs space for some characters + one extra space for a terminating 0. Your string has a length of 1 so it can only fit the terminating 0. Try increasing it's size to say 32.
Also inint
is never initialised before you use it (which is bad) - by some stroke of luck inint
may == guess
before the user even guesses.
Also see Why is the gets function so dangerous that it should not be used?
You could do:
char buffer[32]
fgets(buffer, sizeof(buffer), stdin);
As a safer way with minimal change or you could look at scanf()
. Both are safer than gets
Upvotes: 1
Reputation: 1159
I implemented this using using scanf
; I didn't like the having to allocate a use a char
array to hold the string, and then having to use atoi
to convert to an integer.
Notice that when I'm initializing rand()
, the value that comes after the parentheses is the maximum (minus one). I didn't want zero, so I added one to the number so that our player will be guessing between 1 and 10.
#include <stdio.h>
#include <stdlib.h>
int main(void) {
srand(time(NULL));
int inint = 0;
int guess = rand() % 10 + 1;
while (guess != inint) {
scanf(" %d", &inint);
if(inint > guess){
puts("too high");
} else if (guess>inint){
puts("too low");
} else{
puts("right");
}
}
return 0;
}
As always, let me know if you have any questions.
There was a question about the new line in the input buffer again today with regards to scanf
and the space before the format specifier, so I'll just leave this here.
Upvotes: 0
Reputation: 11921
There are lot of unclear things in your code, first make it clear.Like inint
is not initialized 1st time. Also why instring
size is only 1 byte
. If you entered min 1 char
also you need 2 byte
, plus one for terminating \0
char. So increase the size of instring
.
int main(void){
srand(time(NULL));
char instring[32];/* increase the size */
int inint = 0;/* u forget to initialize */
int guess=rand();
while (guess!=inint){ /* when this condition fails ? */
printf("Guess Number\r\n");
//gets(instring);/* don't use gets(), instead use fgets() */
fgets(instring,sizeof(instring),stdin);
inint = atoi(instring);
if(inint > guess){
puts("too high");
}else if(guess>inint){
puts("too low");
}else{
puts("right");
}
}
return 0;
}
Also read man 2 rand()
& check what it returns. Finally use fgets()
instead of gets()
.
Upvotes: 0