3

I want to be able to do something along the lines of Press any key to exit at program completion, but have no been able to figure out how to.

When I run my program, the terminal exits before I can see the results.

//by Nyxm
#include <stdio.h>

main() {

    int temp, x, flag, num, size;

    printf("\nEnter how many numbers you wish to enter: ");
    scanf("%d", &size);
    int array[size];

    for (x = 0; x < size; x++) {
        printf("Enter an integer: ");
        scanf("%d", &num);
        array[x] = num;
    }

    printf("Please enter either 1 or 2\n1:\tAscending\n2:\tDescending\n\n...");
    scanf("%d", &num);

    if (num == 1) {
        flag = 0;
        while (flag == 0) {
            flag = 1;
            for (x = 1; x < size; x++) {
                if (array[x] < array[x-1]) {
                    flag = 0;
                    temp = array[x];
                    array[x] = array[x-1];
                    array[x-1] = temp;
                }
            }
        }
        } else {
        flag = 0;
        while (flag == 0) {
            flag = 1;
            for (x = 1; x < size; x++) {
                if (array[x] < array[x-1]) {
                    flag = 0;
                    temp = array[x];
                    array[x] = array[x-1];
                    array[x-1] = temp;
                }
            }
        }
    }

    printf("\nYour sorted array:\n");
    for (x = 0; x < size; x++) {
        printf("%d\n", array[x]);
    }
}

Any suggestions?

I am using MonoDevelop in Wubi, if that makes any difference.

RandomSeed
  • 29,301
  • 6
  • 52
  • 87
Nyxm
  • 881
  • 3
  • 11
  • 13
  • 3
    Is your development environment Windows or Linux (or *other*)? – hatboyzero Feb 21 '12 at 22:58
  • 1
    Use a better terminal emulator. – Carl Norum Feb 21 '12 at 22:59
  • 2
    Fix your development environment. The actual program should not have to be encumbered by such things. – Kerrek SB Feb 21 '12 at 23:13
  • What if you get your data ready in a file and redirect input from that file rather than the keyboard: `program < datafile`? Would you still want to wait for a key (remember "keys" come from the file). And what if you redirect output: `program < datafile > resultfile`? In short: **don't do that**. – pmg Feb 22 '12 at 09:35

7 Answers7

21

To do this quick hack, the most common two options are:

/* Windows only */
#include <stdlib.h>

system("pause");

and

/* Cross platform */
#include <stdio.h>

printf("Press enter to continue...\n");
getchar();

I suggest the latter method, though the first method really triggers on "any" key while the bottom one only triggers on enter.

orlp
  • 112,504
  • 36
  • 218
  • 315
  • 1
    Neither of these will work for me. I am using MonoDevelop in Wubi, if that makes any kind of difference. – Nyxm Feb 21 '12 at 23:03
5

Use getchar():

...program...
printf("press enter to continue...\n");
getchar()
Dave
  • 10,964
  • 3
  • 32
  • 54
2

getchar() is the right way to go, but you'll run into problems caused by scanf leaving '\n' in the input buffer - it will return immediately. See Why doesn't getchar() wait for me to press enter after scanf()?

Community
  • 1
  • 1
yachoor
  • 929
  • 1
  • 8
  • 18
1

if we usegetchar();, getch(); or system("pause"); when we open the debug folder and run the file .exe it still exit immediately :)) i use cin >> variable; it certain pause the screen :))

1

Depending on your need and platform, you may use getch() (or _getch()), or ultimately getchar().

The problem with getchar() is that it requires the user to press "enter". The advantage with getchar() is that it is standard and cross-platform.

getch() get all the other property : it just needs a key to be pressed, no display, no "enter" needed. But it's non standard, so support varies depending on platform.

Alternatively, for windows only, there is also :

system("pause");
Cyan
  • 13,248
  • 8
  • 43
  • 78
  • This restriction comes from unix-type terminals typically being in line input mode unless you set them to character mode, at which point getchar() will return at the first character, without waiting for an end-of-line. – Chris Stratton May 22 '13 at 15:51
1

Possible options: 1) system("pause"); 2) getch(); 3) getchar();

webgenius
  • 856
  • 3
  • 13
  • 30
0

It's an old question but thought I'd add a technique I use when testing programs on a Windows box.

Compile program into an exe. And then create a batch script to "wrap" the program along the lines of:

@echo off
foo.exe
pause
exit

which will execute your program as it should be, without any dirty hacks, while allowing you to pause the window and see the output.