Archived

This topic is now archived and is closed to further replies.

PaulB

Help with c++

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites