Help with c++

Started by
12 comments, last by PaulB 22 years, 9 months ago
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; }
Advertisement
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
http://therabbithole.redback.inficad.com
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.
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.
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

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?
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;

system("PAUSE");//add this line here
return 0;
}


Edited by - NuffSaid on July 7, 2001 5:31:48 AM
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
thanks but how do i put the "press any key to continue" at the next line?
system("pause"); automatically adds the line for you.
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Just include stdio.h header file and add getchar(); before return 0 in your main() function. It will wait for your input.

getch() from conio.h will work too.
Hello from my world

This topic is closed to new replies.

Advertisement