Hi .
i am using C++ editor to write my C code.
when i press Ctrl+F9 , the output screen comes and goes without stopping for a while.

Please note that i have included "#include<stdio.h>" and i am using "getchar()" function do stop the screen.

Hi .
i am using C++ editor to write my C code.
when i press Ctrl+F9 , the output screen comes and goes without stopping for a while.

Please note that i have included "#include<stdio.h>" and i am using "getchar()" function do stop the screen.

If you are using turbo C then use getch() not getchar(). Its in the header file conio.h.

If you are using turbo C then use getch() not getchar(). Its in the header file conio.h.

Don't listen to dkalite. getch() is not standard, getchar() is.

As for your problem, my crystal ball tells me your problem is on line 75 of your code... :icon_confused:

Don't listen to dkalite. getch() is not standard, getchar() is.

Thanks for the correction but may I know the hazards or any side-effect of using getch() it in TurboC.

Using getchar() we have to press the enter key but using getch() we can just press any key then why getchar() instead of getch().

Edited 7 Years Ago by dkalita: n/a

when i press Ctrl+F9 , the output screen comes and goes without stopping for a while.

Please note that i have included "#include<stdio.h>" and i am using "getchar()" function do stop the screen.

A previous call to scanf() of getchar() is probably leaving characters in the stream. The getchar() you are using to stop the screen only works if the stream is completely empty. You can fix that by adding a loop that reads and throws away characters until a line break is found. Here is a utility function that can do what you want:

void PauseForUser(int promptUser, int clearStream)
{
    if (clearStream)
    {
        int c;

        while ((c = getchar()) != '\n' && c != EOF)
        {
            /* all work in condition */
        }

        clearerr(stdin);
    }

    if (promptUser) fputs("Press [Enter] to continue", stdout);

    getchar();
}

In your code it might be called as PauseForUser(1, 1); .

may I know the hazards or any side-effect of using getch() it in TurboC.

Turbo C is becoming increasingly less compatible with the rest of the world as it ages, and using getch() is one way to lock your code into a specific compiler. The graphics.h header is another, worse way.

Using getchar() we have to press the enter key but using getch() we can just press any key then why getchar() instead of getch().

IMO that is not a deal breaker for getchar(), and it is not enough of a benefit to add an extra portability concern. Personally, I think this is a trivial thing to argue over, kind of like void main(). It is well known, easy to spot, easy to change, and not likely to cause horrible problems. But because of these things that are well known, easy to spot, and easy to change, they are also easy for coding snobs to act all elitist about. ;)

void PauseForUser(int promptUser, int clearStream)
{
    if (clearStream)
    {
        int c;

        while ((c = getchar()) != '\n' && c != EOF)
        {
            /* all work in condition */
        }

        clearerr(stdin);
    }

    if (promptUser) fputs("Press [Enter] to continue", stdout);

    getchar();
}

In your code it might be called as PauseForUser(1, 1); .

May I impose on you by asking, why clearerr(stdin); is necessary in this particular example? What risks I am not seeing when used without it? Thank you.

May I impose on you by asking, why clearerr(stdin); is necessary in this particular example?

It is there for C99 compatibility.

What risks I am not seeing when used without it?

In C99 getchar() will continue to return EOF if the error or end of file indicators are set on the stream. The last getchar() will not block, and that defeats the whole purpose of the function. :) In C89 I have not found an implementation that has the same behavior has C99, so it will probably work as intended without the call to clearerr(). But there is also no risk of not calling clearerr() in C89. It is better all around to assume the more strict rules of C99 and expect subsequent calls to an input function to fail after EOF or an error. That way your code works now and is future proofed.

just remember that
for windows getch and getchar can be used using the header file <conio.h>
In linux for the same you require <stdlib.h>

for windows getch and getchar can be used using the header file <conio.h>
In linux for the same you require <stdlib.h>

Not quite. getchar() is declared in <stdlib.h>, which is a standard header so every compiler will have it. getch() is typically declared in <conio.h>, but only on some Windows compilers. getch() is not available on Linux except when using the curses library.

Edited 7 Years Ago by Tom Gunn: n/a

Comments
perfect answers
This article has been dead for over six months. Start a new discussion instead.