Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using getchar() in a while loop, prints a statement twice.. how?

Tags:

c

I have a very simple program like this

int main()
{
    int opt;
    int n;
    int flag = 1;
    while(flag)
    {
        printf("m inside while.Press c to continue\n");
        if((opt = getchar())== 'c')
        {
            printf("choose a number\n");
            scanf(" %d",&n);
            switch(n)
            {
            case 0:
                printf("m zero\n");
                break;
            case 1:
               printf("entered one\n");
               break;
            case 3:
               printf("m exit\n");
               flag = 0;
               break;
            }
            printf("m broke\n");
        }
    }
    printf("m out\n");
    return 0;
}

I get output like this:

m inside while.Press c to continue
c
choose a number
1
entered one
m broke
m inside while.Press c to continue
m inside while.Press c to continue
c
choose a number

My doubt is why "m inside while.Press c to continue" gets printed twice after every loop??

Thanks in advance

like image 622
unix_kid Avatar asked Dec 27 '13 13:12

unix_kid


People also ask

How use Getchar multiple times?

Trivially, you can fix it using another getchar() to consume the newline: input1=getchar(); getchar(); input2=getchar(); getchar(); Or you can input two characters at a time. Also, note that getchar() retruns an int .

Why is my print statement printing twice in C?

It's because when you use getchar it returns the next character, but leaves the newline in the input buffer. So the next getchar returns that newline. You should also be careful because getchar actually returns an int and not a char .

What does the function getchar () return at the end of file?

getchar returns (rather obviously) the character it reads, or, if there are no more characters available, the special value EOF (``end of file''). A companion function is putchar, which writes one character to the ``standard output.

Why is Getchar used after scanf?

The C program uses scanf() to get the two resistance values and getchar() to select the circuit configuration. Problems can occur when using getchar() after scanf() due to new-line characters being stored in the keyboard buffer.


3 Answers

This is because of \n character left behind by previous scanf. When you input a number and press Enter key, an additional \n character passed to the standard input buffer. scanf reads that nuber leaving behind \n in the buffer. On next iteration of loop getchar reads \n before pressing any character by you and hence m inside while.Press c to continue printed twice as \n is not c.
Place this snippet of code just after the scanf statement in your while loop to eat up the newline characters

while(getchar() != '\n');  

This will eat up any number of \n.
For more detailed explanation on the behavior of getchar read this answer.
You final code should be

 int main()
{
    int opt;
    int n;
    int flag = 1;
    while(flag)
    {
        printf("m inside while.Press c to continue\n");
        if((opt = getchar())== 'c')
        {
            printf("choose a number\n");
            scanf(" %d",&n);
            while(getchar() != '\n');
            switch(n)
            {
            case 0:
                printf("m zero\n");
                break;
            case 1:
               printf("entered one\n");
               break;
            case 3:
               printf("m exit\n");
               flag = 0;
               break;
            }
            printf("m broke\n");
        }
    }
    printf("m out\n");
    return 0;
}
like image 143
haccks Avatar answered Oct 04 '22 06:10

haccks


After scanf reads the input there is a '\n' still in the buffer you have to clear it otherwise it will be readed by getchar in the next time and as it's != 'c' it will prompt again:

Try this :

        printf("choose a number\n");
        scanf(" %d",&n);
        char c;
        while (c = getchar != '\n' && c != EOF);  // clear the buffer
like image 40
rullof Avatar answered Oct 04 '22 04:10

rullof


while(flag)
{
    printf("m inside while.Press c to continue\n");
    while((opt=getchar()) != '\n') {
    if(opt == 'c')
    {
        printf("choose a number\n");
        scanf(" %d",&n);
        switch(n)
        {
        case 0:
            printf("m zero\n");
            break;
        case 1:
           printf("entered one\n");
           break;
        case 3:
           printf("m exit\n");
           flag = 0;
           break;
        }
        printf("m broke\n");
    }
    }
}
like image 37
user3125280 Avatar answered Oct 04 '22 05:10

user3125280