In the following code snippet
the getchar() doesn't work in the second iteration.Why is that?Code:while(1) { char c; printf("enter y/n"); c=getchar(); if(c=='y') continue; else break; }
In the following code snippet
the getchar() doesn't work in the second iteration.Why is that?Code:while(1) { char c; printf("enter y/n"); c=getchar(); if(c=='y') continue; else break; }
it is working - it reads \n character that is left in the stream
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
Okay..is there any way to override that and keep the loop running until a char input 'n' is entered?
Well the problem is the solution in this case. Read all of the input. One getchar will read the interesting character, and another call will read the \n, which you are free to ignore.
Hmm I may be wrong, but aren't continue and break statements "forbidden" in if-else statements ?
No, the code makes sense.
Code:while forever: if c is y: continue the loop else: break it end while