1

I try to write a program in C using the loop that repeated until a specific character is inputted from the keyboard. Here's my code:

#include <stdio.h>
main ()
{
    char option;
    do
        {
            printf("Enter q to quit: ");
            option = getchar ();
        }
    while (option != 'q');
}

I've also tried with the scanf () but the result always the same. Here the output after I tried to test the program:

Enter q to quit: 3
Enter q to quit: Enter q to quit: 2
Enter q to quit: Enter q to quit: 1
Enter q to quit: Enter q to quit: q

Can anyone explain to me why the "Enter q to quit : " always appear twice and how can I fix it?

L.I.B L.I.B
  • 77
  • 3
  • 8

5 Answers5

4

"Enter q to quit: " appears twice because your input buffer still has the new line character in it when it runs a second time.

Fix:

#include <stdio.h>
int main ()
{
    char option;
    do
    {
        printf("Enter q to quit: ");
        option = getchar ();
        while(getchar() != '\n'); //Enter this line here.
    }
    while (option != 'q');
}
J. Nutt
  • 71
  • 5
3

When you enter q, you press q followed by enter (a new line character as far as C is concerned which is \n).

So when your loop returns to the beginning, '\n' is still in your input buffer and getch() automatically reads this and checks whether that equals q before returning to the beginning of your loop again (hence your text looks like it's printed twice).

Try using fgets like this: fgets (option , 4 , stdin)

You have to make sure you have a character array big enough to hold this though so you should define char option [3]; to hold 'q', the newline character '\n' and the termination character '\0';

fgets is quite a nice solution because it will only store up the the second argument worth of characters and throw away the rest. This means 1) you don't overflow your variables/arrays and 2) you don't have junk left in the input buffer :)

Samidamaru
  • 1,061
  • 7
  • 18
  • You are correct. Thank you, I'll edit this to reflect this. char option[2] works though no? [0] = 'q' and [1] = '\0'. There is no need for a third element in the array. – Samidamaru Nov 13 '15 at 15:30
  • Right! Fair enough. Thanks for clearing that up. C adds far too many characters haha ;). – Samidamaru Nov 15 '15 at 14:20
2

You get it printed twice because when you hit enter, a line feed character \n is appended to stdin.

You can discard that line feed character by adding an extra getchar:

do
{
    printf("Enter q to quit: ");
    option = getchar();
    getchar(); // discard line feed
}while (option != 'q');
Lundin
  • 195,001
  • 40
  • 254
  • 396
  • 1
    I don't think this is a good solution. It assumes there **is** a line feed, and that it is **exactly one byte** long. It wont work if the user enter only the character (using ^D on linux, for example), or if he uses a windows new line (CRLF). – 4rzael Oct 12 '15 at 09:30
  • 1
    @4rzael Sometimes you have to adapt answers to the person asking. The intention is to help a beginner to fix their issue and then move on. It is indeed not 100% portable production level code... – Lundin Oct 12 '15 at 09:42
  • Sure, but I think using a scanf or just a while skipping all \n ad \r isn't too complicated and get better results – 4rzael Oct 12 '15 at 11:19
  • @4rzael: Windows CRLF is converted to a single \n character by the stdio library, so that particular thing is not a problem. – Thomas Padron-McCarthy Nov 13 '15 at 15:40
1

If you hit two keys, you'll read two characters. If you want to read characters, call getchar. If you want to read lines, call some function that reads lines.

David Schwartz
  • 179,497
  • 17
  • 214
  • 278
0

getchar() function first reads from the buffer.If buffer is empty then and then it will read from standard input(i.e.screen).

jkd
  • 90
  • 5