I wrote a little game program in C for memorizing number but the problem is I want the player to get 3 successful attempts before adding another number to memorize it , it works fine except for the first number you need to enter it 4 times and not 3 I don't understand why here is the code with the problem :
if(good >= 0 && good <=3)
{
int number;
int lower=0 ;
int upper=9;
srand(time(NULL));
number = (rand() % ( upper-lower 1)) lower;
printf("%d", number );
random_digits = number;
sleep(1);
printf("\rEnter the number please :");
scanf("%d", &reponse);
if(reponse == random_digits)
{
good ;
}
else
{
printf("Do You Want to Play Again?Y/N:");
scanf(" %c" , &a);
if (a == 'Y' || a == 'y')
{
reponse = random_digits;
good=0;
}
else
{
return ;
}
}
}
As you can try it for yourself it always need 4 good answer before going to higher number (the other only need 3) here is the full code so you can better understand what I am saying :
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <time.h>
void main()
{
int random_digits;
int reponse;
int good=0;
char a;
do{
if(good >= 0 && good <=3)
{
int number;
int lower=0 ;
int upper=9;
srand(time(NULL));
number = (rand() % ( upper-lower 1)) lower;
printf("%d", number );
random_digits = number;
sleep(1);
printf("\rEnter the number please :");
scanf("%d", &reponse);
if(reponse == random_digits)
{
good ;
}
else
{
printf("Do You Want to Play Again?Y/N:");
scanf(" %c" , &a);
if (a == 'Y' || a == 'y')
{
reponse = random_digits;
good=0;
}
else
{
return ;
}
}
}
if(good >3 && good <7)
{
int number;
int lower=10 ;
int upper=99;
srand(time(NULL));
number = (rand() % ( upper-lower 1)) lower;
printf("%d", number );
random_digits = number;
sleep(1);
printf("\rEnter the number please :");
scanf("%d", &reponse);
if(reponse == random_digits)
{
good ;
}
else
{
printf("Do You Want to Play Again?Y/N:");
scanf(" %c" , &a);
if (a == 'Y' || a == 'y')
{
reponse = random_digits;
good=0;
}
else
{
return ;
}
}
}
if(good >6 && good <10)
{
int number;
int lower=100 ;
int upper=999;
srand(time(NULL));
number = (rand() % ( upper-lower 1)) lower;
printf("%d", number );
random_digits = number;
sleep(1);
printf("\rEnter the number please :");
scanf("%d", &reponse);
if(reponse == random_digits)
{
good ;
}
else
{
printf("Do You Want to Play Again?Y/N:");
scanf(" %c" , &a);
if (a == 'Y' || a == 'y')
{
reponse = random_digits;
good=0;
}
else
{
return ;
}
}
}
if(good >9 && good <13)
{
int number;
int lower=1000 ;
int upper=9999;
srand(time(NULL));
number = (rand() % ( upper-lower 1)) lower;
printf("%d", number );
random_digits = number;
sleep(1);
printf("\rEnter the number please :");
scanf("%d", &reponse);
if(reponse == random_digits)
{
good ;
}
else
{
printf("Do You Want to Play Again?Y/N:");
scanf(" %c" , &a);
if (a == 'Y' || a == 'y')
{
reponse = random_digits;
good=0;
}
else
{
return ;
}
}
}
if(good >12 && good <16)
{
int number;
int lower=10000 ;
int upper=99999;
srand(time(NULL));
number = (rand() % ( upper-lower 1)) lower;
printf("%d", number );
random_digits = number;
sleep(1);
printf("\rEnter the number please :");
scanf("%d", &reponse);
if(reponse == random_digits)
{
good ;
}
else
{
printf("Do You Want to Play Again?Y/N:");
scanf(" %c" , &a);
if (a == 'Y' || a == 'y')
{
reponse = random_digits;
good=0;
}
else
{
return ;
}
}
}
if(good >15 && good <19)
{
int number;
int lower=100000 ;
int upper=999999;
srand(time(NULL));
number = (rand() % ( upper-lower 1)) lower;
printf("%d", number );
random_digits = number;
sleep(1);
printf("\rEnter the number please :");
scanf("%d", &reponse);
if(reponse == random_digits)
{
good ;
}
else
{
printf("Do You Want to Play Again?Y/N:");
scanf(" %c" , &a);
if (a == 'Y' || a == 'y')
{
reponse = random_digits;
good=0;
}
else
{
return ;
}
}
}
}while(reponse == random_digits);
printf("Thanks for playing!\n");
sleep(2);
}
Another problem is that the printf("Thanks for playing!\n");
don't appear in the end . And if someone has a better structure for the code that would be less bulky or be visually better I would gladly learn it. As if there is a way to repeat the if statement and not having to have to write it each time? Because it is basically the same statement copy and paste (only the value of good change)
CodePudding user response:
This statement:
if(good >= 0 && good <=3)
works for
good=0
good=1
good=2
good=3
Which is 4 values. That is why you see it four times.
Also please don't indent so much -- it makes it harder to read. The point of indentation is to make it easier to read not harder. In my code reviews I expect coders to be consistent with 2-4 spaces for an indent.