2

I wrote a program using switch case statement and asked for a char for input but it does not ask for the char in the console window but skips it completely

int main() 
{
    float a, b, ans;
    char opr;

    printf("\nGIVE THE VALUES OF THE TWO NUMBERS\n");
    scanf(" %f %f",&a,&b);


    printf("\nGIVE THE REQUIRED OPERATOR\n");   

    //no display(echo) on the screen
    //opr = getch();
    //displays on the screen
    //opr = getche();

    scanf("%c",&opr);

    switch(opr)
    {
        case '+' :
            ans = a+b;
            printf("%f", ans);
            break;          
        case '-' :
            ans = a-b;
            printf("%f", ans);
            break;          
        case '*' :
            ans = a*b;
            printf("%f", ans);
            break;          
        case '/' :
            ans = a/b;
            printf("%f", ans);
            break;
        case '%' :
            ans = (int)a % (int)b;
            printf("%f", ans);
            break;
        default :
            printf("\nGIVE A VALID OPRATOR\n");

    }

    system("pause");        
    return 0;

but when i put a space before %c in the second scanf it works someone was telling something about a whitespace which i found confusing

He said the second scanf is taking the value of \n as a character and if i put a space before %c in the second scanf isn't that a character and doesn't it take the space as the character?

But in this program it does not take the \n as the character

int main() 
{
    char a;
    printf("\ngive a char\n");
    scanf("%c",&a);
    printf("%c",a);

    return 0;
}  

This is really confusing can any on help me i want to learn what is wrong.

HaveNoDisplayName
  • 8,291
  • 106
  • 37
  • 47
psrag anvesh
  • 1,257
  • 2
  • 12
  • 18
  • That is probably it reads in the 'newline' character that you enter after inserting the two numbers. The input works like a buffer queue. It lines up characters. In this case, you told it to read the first number into &a , skip whatever in between, read second number into &b, then read the next character. Since after entering the number into &b, im guessing you hit 'ENTER', so it will read 'newline' into &opr. A quick way to fix this is to add another line of 'scanf("%c",&opr);' immediately after your scanf operation line. – TuanDT Sep 18 '15 at 03:39
  • 2
    Put a space before `%c` in the second `scanf`. https://stackoverflow.com/questions/13275417/why-scanfd-does-not-consume-n-while-scanfc-does – kaylum Sep 18 '15 at 03:40
  • 1
    Don't use `scanf`. http://www.c-faq.com/stdio/scanfprobs.html – jamesdlin Sep 18 '15 at 03:45
  • so when i put enter before what does it do does it skip the newline as a character and neglects the enter before %c and waits for the actual character that the user wants to give? and can you tell me what actually is a whitespace in programming ? – psrag anvesh Sep 18 '15 at 03:46
  • 1
    Whitespace is any "empty" character. Like space and tab. So `scanf(" %c", &opr)`. – kaylum Sep 18 '15 at 03:51
  • White-space characters are space, horizontal tab, new-line, vertical tab, and form-feed, according to "6.4 Lexical elements" in [N1256](http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf). – MikeCAT Sep 18 '15 at 04:07
  • 1
    @Washington Guedes If you use `scanf("%s", a);`, change `a`'s type to `char[2];`. also, `scanf("%1s", a);`, which is with reduced buffer overrun risk is better. – MikeCAT Sep 18 '15 at 04:33
  • @MikeCAT. You are right, I didn't know that. _http://ideone.com/73DgOh_ –  Sep 18 '15 at 12:25

4 Answers4

5

Every time you use scanf with this format :

scanf("%c",&a); 

it leaves a newline which will be consumed in the next iteration. Th last program that you mentioned have only one "scanf". try to use another scanf. you will get the same problem.

so to avoid white spaces you have to write :

 scanf(" %c",&opr); 

the space before the format string tells scanf to ignore white spaces. Or its better to use

getchar();

It will consume all your newline

secretgenes
  • 1,291
  • 1
  • 19
  • 39
1

The second program do take the \n as the character.
Maybe you simply didn't input \n before inputting other characters.

example ( %c in printf is changed to %d to make it clear)

MikeCAT
  • 73,922
  • 11
  • 45
  • 70
1

The problem is that you are leaving the \n entered after the numbers unconsumed and then is read by the second scanf(). If you check the value in opr you'll see it's '\n'.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
-2

Try adding fflush(stdin) before scanf.

RBanerjee
  • 957
  • 1
  • 9
  • 18