scanf error!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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);
            }
            else if(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?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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);
            }
            else if(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.
Last edited on
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".

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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);
            }
            else if(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
Last edited on
> scanf("% c",&again);
Where you put the space matters.

Like
scanf(" %c",&again);
scanf(" %c",&again);

this work the program correctly THANKS!
Topic archived. No new replies allowed.