I have a very simple program like this
int main()
{
int opt;
int n;
int flag = 1;
while(flag)
{
printf("m inside while.Press c to continue\n");
if((opt = getchar())== 'c')
{
printf("choose a number\n");
scanf(" %d",&n);
switch(n)
{
case 0:
printf("m zero\n");
break;
case 1:
printf("entered one\n");
break;
case 3:
printf("m exit\n");
flag = 0;
break;
}
printf("m broke\n");
}
}
printf("m out\n");
return 0;
}
I get output like this:
m inside while.Press c to continue
c
choose a number
1
entered one
m broke
m inside while.Press c to continue
m inside while.Press c to continue
c
choose a number
My doubt is why "m inside while.Press c to continue" gets printed twice after every loop??
Thanks in advance
Trivially, you can fix it using another getchar() to consume the newline: input1=getchar(); getchar(); input2=getchar(); getchar(); Or you can input two characters at a time. Also, note that getchar() retruns an int .
It's because when you use getchar it returns the next character, but leaves the newline in the input buffer. So the next getchar returns that newline. You should also be careful because getchar actually returns an int and not a char .
getchar returns (rather obviously) the character it reads, or, if there are no more characters available, the special value EOF (``end of file''). A companion function is putchar, which writes one character to the ``standard output.
The C program uses scanf() to get the two resistance values and getchar() to select the circuit configuration. Problems can occur when using getchar() after scanf() due to new-line characters being stored in the keyboard buffer.
This is because of \n
character left behind by previous scanf
. When you input a number and press Enter key, an additional \n
character passed to the standard input buffer. scanf
reads that nuber leaving behind \n
in the buffer. On next iteration of loop getchar
reads \n
before pressing any character by you and hence m inside while.Press c to continue
printed twice as \n
is not c
.
Place this snippet of code just after the scanf
statement in your while
loop to eat up the newline characters
while(getchar() != '\n');
This will eat up any number of \n
.
For more detailed explanation on the behavior of getchar
read this answer.
You final code should be
int main()
{
int opt;
int n;
int flag = 1;
while(flag)
{
printf("m inside while.Press c to continue\n");
if((opt = getchar())== 'c')
{
printf("choose a number\n");
scanf(" %d",&n);
while(getchar() != '\n');
switch(n)
{
case 0:
printf("m zero\n");
break;
case 1:
printf("entered one\n");
break;
case 3:
printf("m exit\n");
flag = 0;
break;
}
printf("m broke\n");
}
}
printf("m out\n");
return 0;
}
After scanf reads the input there is a '\n'
still in the buffer you have to clear it otherwise it will be readed by getchar in the next time and as it's != 'c'
it will prompt again:
Try this :
printf("choose a number\n");
scanf(" %d",&n);
char c;
while (c = getchar != '\n' && c != EOF); // clear the buffer
while(flag)
{
printf("m inside while.Press c to continue\n");
while((opt=getchar()) != '\n') {
if(opt == 'c')
{
printf("choose a number\n");
scanf(" %d",&n);
switch(n)
{
case 0:
printf("m zero\n");
break;
case 1:
printf("entered one\n");
break;
case 3:
printf("m exit\n");
flag = 0;
break;
}
printf("m broke\n");
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With