I wrote this program:
#include <conio.h>
#include <stdio.h>
void main()
{
char ch='a';
while(ch!='y'&&ch!='n')
{
printf("\nDo you want to print the output?(y/n)");
scanf("%c",&ch);
}
if(ch=='y')
printf("\n accepted!");
getch();
}
expected output:
Do you want to print the output?(y/n)1
Do you want to print the output?(y/n)5
Do you want to print the output?(y/n)y
accepted!
Instead I get:
Do you want to print the output?(y/n)
Do you want to print the output?(y/n)1
Do you want to print the output?(y/n)
Do you want to print the output?(y/n)5
Do you want to print the output?(y/n)
Do you want to print the output?(y/n)y
accepted!
I don't know that why the sentence"Do you want to print the output?(y/n)" is written twice in output?
scanf("%c",&ch);
leaves a newline char in the input stream which is consumed by the next call to the scanf().