-2

This code executes itself without asking for char "choose" for further processing before the switch statement scanf("%c",&choose); switch(choose).

printf("enter option 1,2,3 or 4 \n\n");
scanf("%c",&choose); // why it not ask to input char
switch(choose)
{
    case '1': case '+':
    {
        printf("enter 1st value\n");
        scanf("%f",&a);
        printf("enter 2st value\n");
        scanf("%f",&b);
        c=a+b;
        printf("%f + %f = %f",a,b,c);
        break;
    }
    case '2': case '-':
    {
        printf("enter 1st value\n");
        scanf("%f",&a);
        printf("enter 2nd value\n");
        scanf("%f",&b);
        c=a-b;
        printf("%f - %f = %f",a,b,c);
        break;
    }
    case '3': case'*':
    {
        printf("enter 1st value\n");
        scanf("%f",&a);
        printf("enter 2nd value\n");
        scanf("%f",&b);
        c=a*b;
        printf("%f * %f = %f",a,b,c);
        break;
    }
}
Jongware
  • 22,200
  • 8
  • 54
  • 100
Afraz Afaq
  • 107
  • 7
  • 1. Have you declare `choose` anywhere? 2. What do you mean by why does it not ask - do you mean there is no text there? – shree.pat18 Jul 11 '14 at 08:01
  • Is there anything else BEFORE this printf/scanf? For example...[another scanf](http://stackoverflow.com/questions/4023643/second-scanf-is-not-working)? – Adriano Repetti Jul 11 '14 at 08:02
  • I've declared char choose but it is not working with switch if change variable char to int choose; then it will work but it will no longer able to store arithmetic signs like +-* – Afraz Afaq Jul 11 '14 at 08:07
  • int main() { int Caltype; char choose; float a,c,b; printf("Hello, \n Choose type of calculator!\n"); printf("1. normal \t 2. scientific\n"); scanf("%d",&Caltype); if(Caltype==1) { printf("what do you want to do:"); printf("\n\n 1.\" + \" 2.\" - \" \n\n 3.\" * \" 4.\" / \" \n\n"); printf("enter option 1,2,3 or 4 \n\n"); scanf("%c",&choose); switch(choose) – Afraz Afaq Jul 11 '14 at 08:10
  • 1
    Follow link in my comment (same - common - problem), ENTER character isn't removed by previous scanf from input buffer. It's also what unwind said in his answer. – Adriano Repetti Jul 11 '14 at 08:21
  • try `scanf(" %c", &choose);` – BLUEPIXY Jul 11 '14 at 08:35

3 Answers3

0

Probably because you've already input something earlier, so that there are non-scanned characters left in the buffer which this "greedy" scanf() then scans without stopping.

You should check the return value of scanf() to learn if it succeeded or not.

And you should switch to using fgets() to read in a whole line, and then parse that. It's much safer and easier to predict, that way.

unwind
  • 391,730
  • 64
  • 469
  • 606
0

I've executed it without any problems (it gets the "choose" char):

#include <stdio.h>
int main(int argc, char *argv ) {
        char choose;
        float a,b,c;
        printf("enter option 1,2,3 or 4 \n\n");
        scanf("%c",&choose); // why it not ask to input char
           switch(choose)
        {
            case '1': case '+':
                {
                            printf("enter 1st value\n");
                            scanf("%f",&a);
                            printf("enter 2st value\n");
                            scanf("%f",&b);
                            c=a+b;
                            printf("%f + %f = %f",a,b,c);
                            break;
                }
            case '2': case '-':
                {
                            printf("enter 1st value\n");
                            scanf("%f",&a);
                            printf("enter 2nd value\n");
                            scanf("%f",&b);
                            c=a-b;
                            printf("%f - %f = %f",a,b,c);
                            break;
                }
            case '3': case'*':
                {
                            printf("enter 1st value\n");
                            scanf("%f",&a);
                            printf("enter 2nd value\n");
                            scanf("%f",&b);
                            c=a*b;
                            printf("%f * %f = %f",a,b,c);
                            break;
                }
        }
        return 0;
}

Probably its something you didn't read before...

I'm guessing that you are doing a similar "scanf("%c",&choose);" before, so the next read you will get the carriage return.

You could try to change the following lines:

scanf("%c",&choose);

for:

scanf("%c%*c",&choose);

...so you discard the \n

If this don't solve your issue, maybe you should provide more code :)

lpg
  • 4,897
  • 1
  • 16
  • 16
0
printf("enter option 1,2,3 or 4 \n\n");
scanf("%c",&choose); 

In your code if you are using any scanf before the above given code, then this behaviour is expected as the previous scanf reads the input leaving \n in the input buffer which is feeded as input to next scanf.

To overcome this issue please use fflush(stream) before scanf(...), this should solve the problem.

 fflush(stdin);

or you can use getchar() function, this will alse solve your the issue.

prince
  • 1,129
  • 1
  • 15
  • 38