i have a problem when i use:

do
{
printf("press only A to continue):\n");
x=getchar();
}while(x!='A');

if i press anything the program prints me the message multiple times

Recommended Answers

All 8 Replies

It says to loop until an 'A' is entered, isn't? Then it will print for as long you do not enter an 'A'
Consider that when you enter 'A' you are pressing as well the ENTER key which is represented as a newline. If you press 'B' plus ENTER, that's two chars buffered in stdin. The loop will not show you that in the first instance (with 'A'), but it will in the second (with 'B').

if i press anything else the program prints me the message twice

Because there are two characters that don't match your condition in the stream. Run your test again and count how many keys you press. I bet you'll discover that one of them is the Enter key.

Narue u are right but what can i do to fix it??

Narue u are right but what can i do to fix it??

Well, you can extract the next character (there will always be one if getchar succeeds). If it's a newline, continue as normal. If it's not a newline, return that character to the stream:

#include <stdio.h>

void getnl(void)
{
    /* Check the stream state */
    if (!feof(stdin) && !ferror(stdin))
    {
        int ch;

        /* Grab the next character */
        if ((ch = getchar()) != '\n')
        {
            /* Put it back if it's not a newline */
            ungetc(ch, stdin);
        }
    }
}

int main(void)
{
    int x;

    do
    {
        printf("press only A to continue:\n");
        x = getchar();
        getnl();
    } while (x != EOF && x != 'A');

    return 0;
}

This isn't the only way, of course. See what else you can come up with.

hi !
you entering two characters so it execute the statement two times.
i recomend you the same program but with few changes.

#include<stdio.h>
#include<conio.h>
void main()
{

	clrscr();
	char x;
	do
	{
		printf("\npress only A to continue : ");
		x=getche();
	}while(x!='A');
	getch();
}

i just replace the getchar() function by getche() .
Now it will execute the statement once.

//yes the problem is only becoz of the unwanted '\n' character in the input stream
//so you can use an extra getchar() to handle it , this wud make ur program to run //well
//if you are using Borland C++ comnpiler you can even use fflush(stdin) instead of //the that extra getchar() ( however it is not recommneded to flushout stdin)

#include<stdio.h>
int main()
{
int x;
do
{
printf("press only A to continue):\n");
x=getchar();
getchar(); //to handle unwanted '\n' character in stdin
}
while(x!='A');
return 0;
}

~
~
~
~
~
~
~

hi !
you entering two characters so it execute the statement two times.
i recomend you the same program but with few changes.

#include<stdio.h>
#include<conio.h>
void main()
{

    clrscr();
    char x;
    do
    {
        printf("\npress only A to continue : ");
        x=getche();
    }while(x!='A');
    getch();
}

i just replace the getchar() function by getche() .
Now it will execute the statement once.

getche() is not a standard C function. It requires conio.h header file.
Same with clrscr. getche() returns an int, therefore char x should be int x.
void main() is against the standard as well. It should return an int unless the program doesn't deal with a host OS.

//yes the problem is only becoz of the unwanted '\n' character in the input stream
//so you can use an extra getchar() to handle it , this wud make ur program to run //well
//if you are using Borland C++ comnpiler you can even use fflush(stdin) instead of //the that extra getchar() ( however it is not recommneded to flushout stdin)

#include<stdio.h>
int main()
{
int x;
do
{
    printf("press only A to continue):\n");
    x=getchar();
    getchar(); //to handle unwanted '\n' character in stdin
}while(x!='A');
return 0;
}

Not quite. Pressing just ENTER will leave you with a prompt blinking at you. fflush(stdin); is not an option regardless of the compiler you are using, since invokes undefined behaviour. Anything can happen when you use it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.