Hi all
I am new to linux and new to C i have a problem in the below program hope some one will help me
Problem
In the below program, if I dont enter y or n it should display 'invalid data' and repeat the loop and display enter y or n. It is working properly but the problem is it displays 2 times!! instead of just one time
enter y or n
m
invalid data
enter y or n
invalid data
enter y or n
Here is the code
#include <stdio.h>
main()
{
char inp, valid='n' ;
while (valid=='n')
{
valid=' ';
puts (" enter y or n");
inp=getchar ();
fflush (stdin);
if (inp=='y')
puts ("valid point");
else if (inp=='n')
puts ("validinput");
else
{
puts ("invalid data");
valid='n';
}
}
}
Bookmarks