Jump to content

  • Log In with Google      Sign In   
  • Create Account

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.

  • You cannot reply to this topic
13 replies to this topic

#1 PaulB   Members   -  Reputation: 122

Like
Likes
Like

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

Sponsor:

#2 KalvinB   Banned   -  Reputation: 102

Like
Likes
Like

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
http://therabbithole.redback.inficad.com

#3 merlin9x9   Members   -  Reputation: 174

Like
Likes
Like

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.

#4 PaulB   Members   -  Reputation: 122

Like
Likes
Like

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.


#5 Ahmad Luqman   Members   -  Reputation: 122

Like
Likes
Like

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



#6 PaulB   Members   -  Reputation: 122

Like
Likes
Like

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?

#7 NuffSaid   Members   -  Reputation: 122

Like
Likes
Like

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;

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


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

#8 PaulB   Members   -  Reputation: 122

Like
Likes
Like

Posted 06 July 2001 - 11:25 PM

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

#9 NuffSaid   Members   -  Reputation: 122

Like
Likes
Like

Posted 07 July 2001 - 06:46 AM

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

#10 flame_warrior   Members   -  Reputation: 122

Like
Likes
Like

Posted 07 July 2001 - 07:25 AM

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.

#11 whereiswez   Members   -  Reputation: 122

Like
Likes
Like

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

#12 PaulB   Members   -  Reputation: 122

Like
Likes
Like

Posted 07 July 2001 - 02:38 PM

Thanks everyone!

#13 flame_warrior   Members   -  Reputation: 122

Like
Likes
Like

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.

#14 whereiswez   Members   -  Reputation: 122

Like
Likes
Like

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.



PARTNERS