• FEATURED
• FEATURED
• FEATURED
• FEATURED
• FEATURED

View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Help with c++

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

13 replies to this topic

### #1PaulB  Members

Posted 06 July 2001 - 09:08 PM

I made a program called "conversion" (don''t flame me for going to gamedev.net, i''ll be in game programming soon) but how come when I run the program, it converts (fahrenheit to celcius) then the window closes automatically w/o me seeing if it worked correctly. Here''s my code: // // Program to convert temperature from Celcius degree // units into Fahrenheit degree units // Fahrenheit = Celcius * (212 - 32)/100 +32 // #include #include int main(int nNumberofArgs, char* pszArgs[]) { // enter the temperature in Celcius:"; int celcius; cout << "Enter the temperature in Celcius:"; cin >> celcius; // calculate conversion factor for celcius // to Fahrenheit int factor; factor = 212 - 32; // use conversion factor to convert Celcius // into Fahrenheit values int fahrenheit; fahrenheit; fahrenheit = factor * celcius/100 + 32; //output the results cout << "Fahrenheit value is:"; cout << fahrenheit; return 0; }

### #2KalvinB  Banned

Posted 06 July 2001 - 09:28 PM

Finished programs automatically exit. You have to keep it from finishing without user input.

What you need to do is add a cin at the end so the user has to hit enter for it to exit.

Ben

### #3merlin9x9  Members

Posted 06 July 2001 - 09:29 PM

When you execute a console app from the Windows shell, there''s nothing Windows will do to keep the console open, unless that app is waiting for input or something like that. If you think about it for long enough, you''ll realize that it makes sense.

Assuming that you''re using VC++, if you run the app from the IDE using the exclamation-point button on the Build toolbar or using the Run command in the Build menu, VC++ should automatically wait for a keypress at the end of the app''s execution.

So, if you have VC++, run the app like that. Otherwise, you have two options:

1) Go to the console (Windows 9x calls it "MS-DOS Prompt", though that is a misleading name since Win32 console apps have nothing to do with DOS; Windows NT/2000 correctly calls it the "Command Prompt") and execute your app from there. Since you opened the console explicitly, it will stay until you enter the command "exit". This is how any console app requiring user interaction should be run when not under development; otherwise, it''s fine to run it from the GUI.

2) Add something to your app to request input from the user at the end of execution. This has the unfortunate side-effect of making the user have to press a key to end the program even under scenario #1, which is usually a bad thing; would you like it if, say, Microsoft Word forced you to press a key before it finally exited?

Hope that helps.

### #4PaulB  Members

Posted 06 July 2001 - 09:37 PM

quote:
Original post by merlin9x9
2) Add something to your app to request input from the user at the end of execution. This has the unfortunate side-effect of making the user have to press a key to end the program even under scenario #1, which is usually a bad thing; would you like it if, say, Microsoft Word forced you to press a key before it finally exited?

thanks but how am I supposed to do that?

P.S. This is my first c++ program. All my other programs were in qbasic.

Posted 06 July 2001 - 10:03 PM

u can keep the code in ur while loop. n can have a dummy value to exit from the while loop n the code. By the way its not the best way to do but its just one way to do that. here is what u can do with ur code...
//
// Program to convert temperature from Celcius degree
// units into Fahrenheit degree units
// Fahrenheit = Celcius * (212 - 32)/100 +32
//
#include
#include

int main(int nNumberofArgs, char* pszArgs[])
{
// enter the temperature in Celcius:";
int celcius;

cout << "Enter the temperature in Celcius n -999 to exit:";
cin >> celcius;

while(celcius != -999)
{
cout << "Enter the temperature in Celcius:";
cin >> celcius;

// calculate conversion factor for celcius
// to Fahrenheit
int factor;
factor = 212 - 32;

// use conversion factor to convert Celcius
// into Fahrenheit values
int fahrenheit;
fahrenheit;
fahrenheit = factor * celcius/100 + 32;

if(celcius != -999){
//output the results
cout << "Fahrenheit value is:";
cout << fahrenheit;
}
}
return 0;
}

Luqi

### #6PaulB  Members

Posted 06 July 2001 - 10:17 PM

Forgot to tell you, I got this from the c++ for dummies book. When I ran the program (book has a cd w/ it) it didn''t exit automatically. Obviously, they should have the same source code, right?

### #7NuffSaid  Members

Posted 06 July 2001 - 10:30 PM

I assume you're using Windows, so this should work

1) Include the stdlib.h file.
2) Just before the return 0 statement, add the line system("PAUSE");

Your program should look something like this

//
// Program to convert temperature from Celcius degree
// units into Fahrenheit degree units
// Fahrenheit = Celcius * (212 - 32)/100 +32
//
#include < iostream.h >
#include < stdlib.h >

int main(int nNumberofArgs, char* pszArgs[])
{
// enter the temperature in Celcius:";
int celcius;
cout << "Enter the temperature in Celcius:";
cin >> celcius;

// calculate conversion factor for celcius
// to Fahrenheit
int factor;
factor = 212 - 32;

// use conversion factor to convert Celcius
// into Fahrenheit values
int fahrenheit;
fahrenheit;
fahrenheit = factor * celcius/100 + 32;

//output the results
cout << "Fahrenheit value is:";
cout << fahrenheit;

return 0;
}

Edited by - NuffSaid on July 7, 2001 5:31:48 AM

### #8PaulB  Members

Posted 06 July 2001 - 11:25 PM

thanks but how do i put the "press any key to continue" at the next line?

### #9NuffSaid  Members

Posted 07 July 2001 - 06:46 AM

system("pause"); automatically adds the line for you.

### #10flame_warrior  Members

Posted 07 July 2001 - 07:25 AM

getch() from conio.h will work too.

### #11whereiswez  Members

Posted 07 July 2001 - 12:55 PM

cant you just at the end before "return 0" put

cin >> celcius

? ..

obvious not gona make use of the value, but just so that it waits for you to press enter

### #12PaulB  Members

Posted 07 July 2001 - 02:38 PM

Thanks everyone!

### #13flame_warrior  Members

Posted 07 July 2001 - 07:25 PM

quote:
Original post by whereiswez
cant you just at the end before "return 0" put

cin >> celcius

? ..

obvious not gona make use of the value, but just so that it waits for you to press enter

Very bad idea to even try to accept an input when all you want it to do is pause till you press a key. If you add cin >> celcius it will keep trying to accepting an input till explicitly give it one, enter key won''t work. getch() and getchar() however will accept the enter key. system("PAUSE"); is fine too, but I prefer to keep away from system(char *); command.

### #14whereiswez  Members

Posted 08 July 2001 - 04:12 AM

oh i c .. cool .. well now i know too ..

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.