#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int guess(void);
int main(){
srand(time(NULL));
int y,input;
char again='y';
while(again=='y'){
y=guess();
printf("I have a number between 1 and 1000.\n");
printf("Can you guess my number?\n");
printf("Please type your first guess.\n");
scanf("%d",&input);
while(input!=y){
if(input<y){
printf("Too low. Try again.\n");
scanf("%d",&input);
}
elseif(input>y){
printf("Too high. Try again.\n");
scanf("%d",&input);
}
}
printf("Excellent! You guessed the number!\n");
printf("Would you like to play again (y or n)?\n");
scanf("%c",&again);
scanf("%c",&again);
}
return 0;
}
int guess(void){
int x;
x= 1+rand()%1000;
printf("%d\n",x);
return x;
}
122
I have a number between 1 and 1000.
Can you guess my number?
Please type your first guess.
122
Excellent! You guessed the number!
Would you like to play again (y or n)?
y
212
I have a number between 1 and 1000.
Can you guess my number?
Please type your first guess.
212
Excellent! You guessed the number!
Would you like to play again (y or n)?
n
Process returned 0 (0x0) execution time : 13.385 s
Press any key to continue.
when I delete the last scanf the program doesn't execute scanf but when I duplicate it , program execute scanf! WHY?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int guess(void);
int main(){
srand(time(NULL));
int y,input;
char again='y';
while(again=='y'){
y=guess();
printf("I have a number between 1 and 1000.\n");
printf("Can you guess my number?\n");
printf("Please type your first guess.\n");
scanf("%d",&input);
while(input!=y){
if(input<y){
printf("Too low. Try again.\n");
scanf("%d",&input);
}
elseif(input>y){
printf("Too high. Try again.\n");
scanf("%d",&input);
}
}
printf("Excellent! You guessed the number!\n");
printf("Would you like to play again (y or n)?\n");
scanf("%c",&again);
}
return 0;
}
int guess(void){
int x;
x= 1+rand()%1000;
printf("%d\n",x);
return x;
}
837
I have a number between 1 and 1000.
Can you guess my number?
Please type your first guess.
837
Excellent! You guessed the number!
Would you like to play again (y or n)?
Process returned 0 (0x0) execution time : 3.487 s
Press any key to continue.
It looks like there is a possible whitespace character left in the input buffer. Remember that the "%c" specifier doesn't skip leading whitespace by default. You can force skipping whitespace by placing a space before the 'c' in the specifier "% c".
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int guess(void);
int main(){
srand(time(NULL));
int y,input;
char again='y';
while(again=='y'){
y=guess();
printf("I have a number between 1 and 1000.\n");
printf("Can you guess my number?\n");
printf("Please type your first guess.\n");
scanf("%d",&input);
while(input!=y){
if(input<y){
printf("Too low. Try again.\n");
scanf("%d",&input);
}
elseif(input>y){
printf("Too high. Try again.\n");
scanf("%d",&input);
}
}
printf("Excellent! You guessed the number!\n");
printf("Would you like to play again (y or n)?\n");
scanf("% c",&again);
}
return 0;
}
int guess(void){
int x;
x= 1+rand()%1000;
printf("%d\n",x);
return x;
}
905
I have a number between 1 and 1000.
Can you guess my number?
Please type your first guess.
905
Excellent! You guessed the number!
Would you like to play again (y or n)?
y
158
I have a number between 1 and 1000.
Can you guess my number?
Please type your first guess.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
Too high. Try again.
....
it worked but with a problem!
when I input 'y' it starts infinite loop