# Solved: Quick C Question



## sportscrazy (Nov 27, 2004)

I recently started to learn C from "The Art of Programming; Computer Science With C" and I've been understanding all the material that is in the book. Then I compiled a program to convert inches into feet but the problem is the console window exits before the user gets a chance to see the final output of the program. All along I've been using the


> scanf(" \n");


 to pause the program, so I was wondering is there any other command to stop the console from exiting?


> #include
> 
> void main(void)
> {
> ...


Any help will be appreciated,
Thanks in advance!


----------



## covert215 (Apr 22, 2006)

i'm not entirely sure (c isn't my language), but i think there is a method pause()

it pauses the program until there is input


----------



## sportscrazy (Nov 27, 2004)

It didn't quite work, the compiler came up with these errors:
"missing prototype for pause"
"undefined reference to _pause"

Thank you covert215 for trying! 
Any other suggestions?


----------



## Shadow2531 (Apr 30, 2001)

```
#include <stdio.h>

void pause() {
    fflush(stdin);
    printf("\npress ENTER to exit . . . ");
    getchar();
}

int main() {
    float inches;
    printf("How many inches would you like to be converted into feet? ");
    scanf("%f", &inches);
    printf("\nThere are %f feet in %f inches.\n", inches / 12, inches);
    pause();
    return 0;
}
```


----------



## sportscrazy (Nov 27, 2004)

No luck Shadow2531, still says "missing prototype for pause".

EDIT:

Never mind, it was just my compiler being stupid, I just had to force it to compile, and it works, thank you!


----------



## coderitr (Oct 12, 2003)

There is also a system( ) function with which you can pass any command to the interpreter. It is resource intensive and very rarely used professionally but for a learner it's okay.


```
system( "pause" );
```
I've never heard of a pause( ) function.


----------



## sportscrazy (Nov 27, 2004)

What would professionals use, because all the programs that have been given in this book exit before I get a chance to see the results, so this author didn't use anything at all. Also is C just really good for console applications rather than window apps.?

PS. Thank you, system("pause"); works nicely!


----------



## covert215 (Apr 22, 2006)

C is a little outdated. C++ and C# are the way to go.


----------



## Shadow2531 (Apr 30, 2001)

sportscrazy said:


> What would professionals use?


In general, they wouldn't use anything. It's a command line program. Just load it from the command line like you're supposed to.

Using system() in general is fine when you actually *need* to call a certain program, but there's no need to call pause when you're program can ask for input itself.

Also pause is generally only available on windows, so if you use system("pause"), you're code won't be portable.

The thing people like about system("pause") is that you can press any key instead of having to press enter. You can still simulate that in your program by using getch(), but getch() isn't normally available to linux, so you're code won't be portable again unless you use the getch in the ncurses library for example.

Point being, pause by asking for input using standard methods like getchar() and make the user hit enter. You just have to make sure the stdin buffer is clean first or the getchar() will suck in a char and you're program will exit.

Point being, if you are just concerned about windows, getch() is better than system("pause");

getch() is usually available in conio.h. If not, you can import it yourself and link to msvcrt.dll if necessary. (using mingw for the example below).


```
#include <stdio.h>
_CRTIMP int __cdecl getch (void); /* Or use conio.h instead if you have it */

void pause() {
    fflush(stdin);
    printf("\nPress any key to continue . . . ");
    getch();
    printf("\n");
}

int main() {
    pause();
    return 0;
}
```
Also, for a cross-platform suggestion, see:
http://forums.techguy.org/3404992-post21.html


----------



## sportscrazy (Nov 27, 2004)

covert215 said:


> C is a little outdated. C++ and C# are the way to go.


I just started learning C as something to keep me busy over the summer, also I had the book to learn C so it was quick access. I will go into either C++ or Java after I finish C because next year in school I took computer science and the teacher said that they use Java, so maybe Java is the more sensible choice. Thank you, covert215! 

Shadow2531:
Thank you for all you help and patience, I appreciate it!


----------



## lotuseclat79 (Sep 12, 2003)

Hi sportscrazy,

Assuming you are executing the compiled C program from a command prompt window, i.e. Start>Run>cmd>OK on a WinXP computer, then you should be able to issue a help command for a list of commands one of which will tell you how to keep the program generated window from closing too early before its output can be viewed.

So, the problem you are experiencing is not related to C, but to the shell you are using to invoke the program. Sustaining the program generated window with the output is simply a matter of invoking the program with the proper shell parameter active.

Can't access my Windows platform at the moment to demonstrate how to do this - i.e. I'm posting this from my Linux platform.

-- Tom


----------



## Shadow2531 (Apr 30, 2001)

cmd /K program.exe
cmd /C program.exe

Should be one of those. I also cannot test at the moment.


----------

