Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,132 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,797 people online right now. Registration is fast and FREE... Join Now!




Command prompt window closes after user input

 
Reply to this topicStart new topic

Command prompt window closes after user input

crovaxpsodc
23 Mar, 2007 - 10:21 AM
Post #1

New D.I.C Head
*

Joined: 23 Mar, 2007
Posts: 9


My Contributions
I've only recently started programming with C, and this problem has come up a few times. My code compiles fine, but when I run the .exe and enter my input in the command prompt window, I can see the next line for a fraction of a second before the window closes. Here is an example:

CODE

#include <stdio.h>

void print_converted(int pounds)
/* Convert U.S. Weight to Imperial and International
   Units. Print the results */
{       int stones = pounds / 14;
        int uklbs = pounds % 14;
        float kilos_per_pound = 0.45359;
        float kilos = pounds * kilos_per_pound;

        printf("   %3d          %2d  %2d        %6.2f\n",
                pounds, stones, uklbs, kilos);
}

main()
{       int us_pounds;

        printf("Give an integer weight in Pounds : ");
        scanf("%d", &us_pounds);

        printf(" US lbs      UK st. lbs       INT Kg\n");
        print_converted(us_pounds);
}


When I enter a number for the pounds, say 20, I can see the next line appear but then the window immediately closes. If anyone can offer a solution to this, I would greatly appreciate it.
User is offlineProfile CardPM
+Quote Post

tody4me
RE: Command Prompt Window Closes After User Input
23 Mar, 2007 - 10:35 AM
Post #2

Only Jenny Craig makes thin clients...
Group Icon

Joined: 12 Apr, 2006
Posts: 1,278



Thanked: 3 times
Dream Kudos: 100
My Contributions
add a cin.getline() to the end of the program. This will accept another line of input that is ignored before the program exists.
User is offlineProfile CardPM
+Quote Post

crovaxpsodc
RE: Command Prompt Window Closes After User Input
10 Apr, 2007 - 03:57 PM
Post #3

New D.I.C Head
*

Joined: 23 Mar, 2007
Posts: 9


My Contributions
That didn't seem to work. I guess I should be more specific. When I write and compile programs using Microsoft C++ 6.0, the program works fine. However, when I use DevCpp, the command window closes immediately after input.
User is offlineProfile CardPM
+Quote Post

mizzfitcenny
RE: Command Prompt Window Closes After User Input
10 Apr, 2007 - 05:39 PM
Post #4

New D.I.C Head
*

Joined: 13 Jan, 2007
Posts: 13


My Contributions
QUOTE(crovaxpsodc @ 10 Apr, 2007 - 04:57 PM) *

That didn't seem to work. I guess I should be more specific. When I write and compile programs using Microsoft C++ 6.0, the program works fine. However, when I use DevCpp, the command window closes immediately after input.



I'm not sure b/c I'm a newbie at C++ myself, but I use Dev C++ and in order to get the window to stay up when you compile you need...
system("PAUSE"); and that's right before your return 0;
User is offlineProfile CardPM
+Quote Post

crovaxpsodc
RE: Command Prompt Window Closes After User Input
10 Apr, 2007 - 06:09 PM
Post #5

New D.I.C Head
*

Joined: 23 Mar, 2007
Posts: 9


My Contributions
I tried that and it gives me an error along the lines that system was not a declared variable. I think we're technically using C instead of C++, but my professor said that C++ compilers would work (hence the use of Microsoft C++ in the classroom).
User is offlineProfile CardPM
+Quote Post

Amadeus
RE: Command Prompt Window Closes After User Input
10 Apr, 2007 - 06:31 PM
Post #6

g++ -o drink whiskey.cpp
Group Icon

Joined: 12 Jul, 2002
Posts: 12,226



Thanked: 37 times
Dream Kudos: 25
My Contributions
The command window closes because the program has terminated...to hold it open, you need something to do so. It does not occur in Visual Studio because VS uses it's own command window, not the system command shell.

If you use the system("pause"); command, you will need to include the proper library. For the C language, that library is <stdlib.h>. This method is platform dependant, however.

You could also use the getchar() function to hold for user input before closure.
User is offlineProfile CardPM
+Quote Post

crovaxpsodc
RE: Command Prompt Window Closes After User Input
10 Apr, 2007 - 06:44 PM
Post #7

New D.I.C Head
*

Joined: 23 Mar, 2007
Posts: 9


My Contributions
That works, thanks everyone for the help!
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/1/08 10:24PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month