sorry, my eng is not good that much
I am trying to run this program I am getting a strange result when I press 'a' through keyboard switch is running twice automatically for first time it is going to perfect case then second time it's going to default case

main()
{
  int grade;
  int acount =0;
  int bcount =0;
 while((grade=getchar())!=EOF)
 {
     switch(grade)
    {
        case 'a': ++acount; break;
        case 'b': ++bcount; break;
        default:   printf("you enter wrong choice"); 
    }
  }
return 0;
}

Recommended Answers

All 8 Replies

To get a single character you have to flush the '\n' character from the keyboard buffer. Here is one way to do that

#include <stdio.h>
int main()
{
  int grade;
  int acount =0;
  int bcount =0;
 while((grade=getchar())!=EOF)
 {
     switch(grade)
    {
        case 'a': 
            ++acount; 
            printf("account = %d\n", acount); 
            getchar();
            break;
        case 'b': 
            ++bcount; 
            printf("bccount = %d\n", bcount);
            getchar();
            break;
        default:   printf("you enter wrong choice"); 
    }
  }
return 0;
}

the short answer is because your loop, "while ... getchar" is also getting, and processing, the newline character

one thing to consider though, is "what if" the person enters more than one character ...

DRAGON's example of just a single "getchar" will not handle that... to do so, you would need to replace the single "getchar" within each case statement with something like: do { flush = getchar(); } while (flush && flush != '\n'); .

Thank u very much for this,

Why we are putting getchar() again
If it is not what happening there
please give me reason

Thank u very much for this,

Why we are putting getchar() again
If it is not what happening there
please give me reason

My guess:
getchar() only reads one character at a time from the standard input buffer ( stdin ).
However by virtue of the nature of the buffer when you enter `a' the buffer is holding "a\n" the `a' you entered and the ENTER key.
If you use getchar() inside some form of loop, that extra ENTER key is going to count as a read character in the next loop, which is an unintended result I assume.
An extra call to getchar() will take care of the buffered `newline\return'.
Unpractical solution, since it only works in some `controlled' scenarios.

Thank u very much for this,

Why we are putting getchar() again
If it is not what happening there
please give me reason

Let's say you type in one letter 'a' and press Enter, at that point, in the input buffer are two characters, the letter 'a' plus the newline character '\n'.
Now your program reads the letter 'a' and removes it from the input buffer. But, the newline character '\n' is still there waiting to be processed and your while() loop automatically reads the '\n' - hence your program prints "you enter wrong choice" when you don't expect it to do that.

So, you must somehow handle the '\n'. You can either remove it by means of extra getchar() call(s) like already shown above, or you could add a case to your switch, i.e.

main()
{
  int grade;
  int acount =0;
  int bcount =0;
 while((grade=getchar())!=EOF)
 {
     switch(grade)
    {
 [B]       case 'n': [/B]
             // the newline, do nothing here, just break ...
             break;
        case 'a': ++acount; break;
        case 'b': ++bcount; break;
        default:   printf("you enter wrong choice"); 
    }
  }
return 0;
}

Edit: Oh well, didn't see Aia's post ...

Thank u very much dude

this is very helpful to me

except that case 'n': should be case '\n': a simple typo, of course, but one that could drive a beginner to pull their hair out.

just sayin'.

commented: well spotted ... thanks +3

[i m ma]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.