0
#include<stdio.h> #include<stdlib.h> int main() { char c='y'; int n=0; while(c=='y') { printf("this is loop number %d\n", n); n++; printf("do you wish to continue? (y/n)"); c=getchar(); } return 0; } 

The while loop is exiting after the initial iteration and the getchar is not taking the character input as expcted! what's wrong with the implementation here.

20
  • 1
    use getche() instead of getchar() Commented Jan 15, 2017 at 1:01
  • 1
    @李哲源ZheyuanLi You never want scanf. Commented Jan 15, 2017 at 1:03
  • 1
    @DivyanshuSrivastava Interesting. What's in c at the end? Commented Jan 15, 2017 at 1:05
  • 1
    @Mohsen_Fatemi Neither of those are standard C, and they don't exist on my system. Commented Jan 15, 2017 at 1:06
  • 3
    @Mohsen_Fatemi We all know what <conio.h> is, but that's Microsoft specific and won't work on another platform because it's not standardised Commented Jan 15, 2017 at 1:14

2 Answers 2

2

When the user presses Enter, two characters are generated: 'y' or 'n' and a newline character '\n'. The first call to getchar() gets the letter, the next call gets the newline. Your code must ignore all newlines.

do { c = getchar(); } while(c == '\n'); 
Sign up to request clarification or add additional context in comments.

8 Comments

@Stargateur To eliminate all newlines, as many of them may be in the keyboard buffer.
I understand this but why use a do; while; loop instead of a while; loop? It's just a question not a critic. You have an issue if getchar() return EOF. You should change your answer to fix this issue.
@Stargateur You must get c first and only then decide whether to keep it or not. With while, you must check the condition before executing the action. As for EOF, getchar() returns it only at the end of file, which happens only when the program is terminated by ^C or ^D, or at keyboard error.
getchar() is standard ?
@Mohsen_Fatemi Yes it is: CONFORMING TO C89, C99, POSIX.1-2001.
|
0

When you type "y" with enter on your terminal, you have entered two characters. 'y' and '\n' so getchar() return '\n' on the second call. Plus getchar() return a int not a char but you stock it in char c.

#include <stdio.h> #include <stdlib.h> int main() { int c = 'y'; int n = 0; while (c == 'y') { printf("this is loop number %d\n", n); n++; printf("do you wish to continue? (y/n)"); while ((c = getchar()) == '\n' && c != EOF); } return 0; } 

You can use scanf() because getchar() is very limited.

#include <stdio.h> #include <stdlib.h> #include <stdbool.h> int main(void) { int n = 0; while (true) { printf("this is loop number %d\n", n++); printf("do you wish to continue? (y/n)\n"); char c; if (scanf(" %c", &c) != 1 || c != 'y') { break; } } } 

Or:

#include <stdio.h> #include <stdlib.h> int main(void) { int n = 0; { char c; do { printf("this is loop number %d\n", n++); printf("do you wish to continue? (y/n)\n"); } while (scanf(" %c", &c) == 1 && c == 'y'); } } 

7 Comments

@BLUEPIXY Why? Do you prefer " %c"?
@BLUEPIXY You are wrong ideone.com/Qnra1G. Or explain yourself more clearly because you don't help here i will report your comment like spam. If you want troll someone don't choice me because I will win. It's not because you have a lot of reputation that you can be unpleasant.
@BLUEPIXY After try to fix it, I think "this is not an issue...". So I don't care. The OP never ask that '\n' must stop the loop and the first answer has the same behavior.
A trouble with (scanf("%c\n", &c) is that on its first call and the user types <x>, <Enter> , the function does not return right away. The '\n' tells scanf() to consume all white space until some non-white space is found. With buffered input, that means another line of input like <y>, <Enter> scanf() sees the y, puts it back into stdin and returns 1 with x in c. So the user had to type 2 lines of input to read one character on the first call.
@Stargateur \n in scanf format string does not do what you think it does, please read the manual. The ideone example does not illustrate the problem because ideone does not have interactive input
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.