Screen pause at program end in C

I want to do something line by line Press any key to exitat the end of the program, but could not figure out how to do it.

When I run my program, the terminal exits before I 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 use in , if that matters. MonoDevelop Wubi

+2
source share
7 answers

To make this quick hack, two options are most common:

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

system("pause");

and

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

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

I suggest the last method, although the first method really launches "any" key, and the bottom only starts when you enter.

+18
source

Use getchar():

...program...
printf("press enter to continue...\n");
getchar()
+4
source

getch() ( _getch()) , , getchar().

getchar() , "enter". getchar() , -.

getch() : , , , "enter". , .

, :

system("pause");
+1

getchar() - , , scanf, '\n' - . . getchar() enter scanf()?

+1

: 1) ( "" ); 2) getch(); 3) getchar();

+1

This is an old question, but I thought I would add a method that I use when testing programs in a Windows window.

Compile the program in exe. Then create a script package to wrap the program line by line:

@echo off
foo.exe
pause
exit

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

0
source

if we use getchar();, getch();or system("pause");when we open the debug folder and run the .exe file, it still ends immediately :)) I use cin >> variable;this certain pause of the screen :))

0
source

All Articles