Search code examples
cwindowsconsolevisual-studio-2015windows-console

How to make the console output window stay open in Visual Studio 2015 stay open when extra input is given?


I wrote the following program in Visual Studio 2015-

#include <stdio.h>
#define PRAISE "You are an extraordinary being."
int main(void)
#pragma warning(disable : 4996)
{
    char name[40];

    printf("What's your name? ");
    scanf("%s", name);
    fflush(stdin);
    printf("Hello, %s. %s\n", name, PRAISE);

    getchar();
    getchar();
    getchar();
    getchar();

    return 0;
}

The program runs fine in all cases. But the console window stays open when I provide "Shabbir", "Shabbir K", and "Shabbir Kh" as inputs. The console window won't stay open if I provide "Shabbir Kha" as input.

As you can see, the console window stays open until the second word reaches three character length. It doesn't stay open if the second word is three or more characters long.

The same program doesn't show any problems in Code Blocks 13.12.


Solution

  • to keep the window open:

    while( getchar() != EOF );
    getchar();