patterncMinor
"Guess the Number" game in C
Viewed 0 times
numbergametheguess
Problem
This is my first program in C. I'd rather not form any bad habits now. Is there anything that looks like bad practice, or something just looks wrong?
#include
#include
#include
int main(void) {
srand(time(NULL));
int r = rand() % 10 + 1;
int correct = 0;
int guess;
int counter = 0;
printf("Guess my number! ");
do {
scanf("%d", &guess);
if (guess == r) {
counter++;
printf("You guessed correctly in %d tries! Congratulations!\n", counter);
correct = 1;
}
if (guess r) {
counter++;
printf("Your guess is too high. Guess again. ");
}
} while (correct == 0);
return 0;
}Solution
Here are some of the improvements i can suggest:
-
Use
warning: ISO C90 forbids mixed declarations and code [-pedantic]
So to ensure portability , avoid statements between declarations.
-
In the long run , more meaningful and formal names , can be used instead of names like
In my personal opinion , these two changes would be better:
-
Using a
-
I would prefer to use just a
Here is the program , modified with the changes , i listed above:
- move your
countervariable , from the inside theifstatements , as increasing the count is independent of theifcondition.
-
Use
srand(time(NULL)); , below the declarations , or else , your compiler may throw this warning , when compiled using C90 standard.warning: ISO C90 forbids mixed declarations and code [-pedantic]
So to ensure portability , avoid statements between declarations.
-
In the long run , more meaningful and formal names , can be used instead of names like
r and guess.In my personal opinion , these two changes would be better:
-
Using a
break; , when the correct number is guessed , can eliminate the need for using an extra variable correct.-
I would prefer to use just a
while() instead of do{}while();, if the program logic permits it.Here is the program , modified with the changes , i listed above:
#include
#include
#include
int main(void)
{
int random_num = 0;
int guessed_num = 0;
int counter = 0;
srand(time(NULL));
random_num = rand() % 10 + 1;
printf("Guess my number! ");
while(1)
{
counter++;
scanf("%d", &guessed_num);
if (guessed_num == random_num)
{
printf("You guessed correctly in %d tries! Congratulations!\n", counter);
break;
}
if (guessed_num random_num)
printf("Your guess is too high. Guess again. ");
}
return 0;
}Code Snippets
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main(void)
{
int random_num = 0;
int guessed_num = 0;
int counter = 0;
srand(time(NULL));
random_num = rand() % 10 + 1;
printf("Guess my number! ");
while(1)
{
counter++;
scanf("%d", &guessed_num);
if (guessed_num == random_num)
{
printf("You guessed correctly in %d tries! Congratulations!\n", counter);
break;
}
if (guessed_num < random_num)
printf("Your guess is too low. Guess again. ");
if (guessed_num > random_num)
printf("Your guess is too high. Guess again. ");
}
return 0;
}Context
StackExchange Code Review Q#24366, answer score: 7
Revisions (0)
No revisions yet.