Switch statement not scanning input

I want to scan what the user inputs in case'1' however when executed
I input the number 1 for the first option.
However it just prints "please enter the letter Y and the "if" printf statement, straight after one another without letting the user entering an input.

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
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main()
{
    char Yes;
    char No;
    char choice;
    
    printf("\nC would like to know if he is liked");
    printf("\n1.I like C :)");
    printf("\n2.I do not like C :(\n");
    
    choice=getchar();
    
    switch(choice)
    {
                  case'1':
                          printf("\nPlease enter the letter Y:");
                          scanf("%c",&Yes);
                          
                          if (Yes=='Y' || 'y')
                          printf("\nHe likes you too\n");
                          else 
                               printf("Error");
                          break;
                             
                          
                  case'2':
                          printf("\nPlease enter the letter N: ");
                          scanf("%c", &No);
                          
                          if (No=='N' || 'n')
                             printf("\nHe does not like you too\n");
                             break;
                  
                  default:
                          printf("\n Option does not exist\n");
    }
    
    system("pause");
    
}
Apparently the getchar() does not read a character before you hit Enter. When you do hit it, getchar takes one character and leaves the newline character into the stdin. Then cometh scanf, which is happy with that character.

One thing that apparently "behaves" is scanf(" %c",&Yes);. The space in format makes scanf discard all whitespace, including that newline, that precedes non-whitespace character.
However I have used this style to write a different program which works like a charm e.g.
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
46
47
48
49
50
51
52
53
54
55
56
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int main()
{
    float numberA;
    float numberB;
    char choice;
    
    printf("Please choose one of the following options:");
    printf("\n\t1.Addition");
    printf("\n\t2.Subtraction");
    printf("\n\t3.Multiplication");
    printf("\n\t4.Division\n\n\t");
    
    printf("I choose: ");
    choice=getchar();
    
    switch(choice)
    {
                  case'1':
                          printf("\nEnter your first number: ");
                          scanf("%f", &numberA);
                          printf("\nEnter your second number: ");
                          scanf("%f", &numberB);
                  
                          printf("\n%f + %f = %.3f\n",numberA, numberB, numberA+numberB);
                  
                  case'2':
                          printf("\nEnter your first number: ");
                          scanf("%f", &numberA);
                          printf("\nEnter your second number: ");
                          scanf("%f", &numberB);
                          
                          printf("\n%f - %f = %.3f\n", numberA, numberB, numberA-numberB);
                  
                  case'3':
                          printf("\nEnter your first number: ");
                          scanf("%f", &numberA);
                          printf("\nEnter your second number: ");
                          scanf("%f", &numberB);
                          
                          printf("\n%f * %f = %.3f\n", numberA, numberB, numberA*numberB);
                          
                  case'4':
                          printf("\nEnter your first number: ");
                          scanf("%f", &numberA);
                          printf("\nEnter your second number: ");
                          scanf("%f", &numberB);
                          
                          printf("\n%f / %f = %.3f\n", numberA, numberB, numberA/numberB);
                  
    }
    system("pause");
}
Different indeed.

man scanf:
c ... The usual skip of leading white space is suppressed. To skip white space first, use an explicit space in the format.

In other words: %f skips whitespace and reads a float, while %c takes the first character, even if isspace().
Sorry man now I understand.
Sometimes I am a bit slow in understanding programming.
All fixed cheers :)

 
" %c"
Topic archived. No new replies allowed.