RSS Forums RSS
Please support our C++ advertiser: Programming Forums
Views: 4305 | Replies: 13
Reply
Join Date: Sep 2004
Location: Delaware, USA
Posts: 417
Reputation: Young Teck 06 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Young Teck 06's Avatar
Young Teck 06 Young Teck 06 is offline Offline
Street Game CEO

Solution Keeping Program Execution Going

  #1  
Oct 24th, 2004
Need a little help here. Seems as if whenever I run the program, it closes before showing the results.

Here is the code below:

//
// Program to convert temperature from Celsius degree
// units into Fahrenheit degree units:
// Fahrenheit = Celsius  * (212 - 32)/100 + 12
//
#include <stdio.h>
#include <iostream.h>
 
int main(int nNumberofArgs, char* pszArgs[])
{
		// enter the temperature in Celscius
		int celsius;
		cout << "Enter the temperature in Celcius:";
		cin >> celsius;
 
		// calculate conversion factor for Celsius to Fahrenheit
		int factor;
		factor = 212 - 32;
 
		// use conversion factor to convert Celsius into Fahrenheit values
		int fahrenheit;
		fahrenheit = factor * celsius/100 + 32;
 
		// output the results
		cout << "Fahrenheit value is:";
		cout << fahrenheit;
 
		return 0;
}

What can I do to keep the program running using Command Prompt.
Need Website Work... PM Me or EMail Me at mdstreetsoulja@gmail.com ... I am AVAILABLE!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2004
Location: Lahore
Posts: 10
Reputation: Ejaz is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Ejaz's Avatar
Ejaz Ejaz is offline Offline
Newbie Poster

Re: Keeping Program Execution Going

  #2  
Oct 24th, 2004
What compiler are you using? I had this problem way back, when I was in learning phase and used Turbo C++, and I had to stop it manually, in MS Visual C++ console applications, it is handled automatically.

You can use
system("pause");

in the end or its equivalent to stop it. (and if it didn't work, then you can do what I did, I used getch() and displayed a messaged myself when I used Turbo C 3.0 )
Regards,

Ejaz.
Reply With Quote  
Join Date: Sep 2004
Location: Delaware, USA
Posts: 417
Reputation: Young Teck 06 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Young Teck 06's Avatar
Young Teck 06 Young Teck 06 is offline Offline
Street Game CEO

Re: Keeping Program Execution Going

  #3  
Oct 24th, 2004
I use GNU C++ Compiler, really need to get MS Visual C++ so everybody says its EXCELLENT.
Need Website Work... PM Me or EMail Me at mdstreetsoulja@gmail.com ... I am AVAILABLE!
Reply With Quote  
Join Date: Sep 2004
Location: Delaware, USA
Posts: 417
Reputation: Young Teck 06 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Young Teck 06's Avatar
Young Teck 06 Young Teck 06 is offline Offline
Street Game CEO

Re: Keeping Program Execution Going

  #4  
Oct 24th, 2004
Unfortunately pal, that didnt work, sucks I know. Any other method?
Need Website Work... PM Me or EMail Me at mdstreetsoulja@gmail.com ... I am AVAILABLE!
Reply With Quote  
Join Date: Sep 2004
Posts: 7
Reputation: pratima is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
pratima pratima is offline Offline
Newbie Poster

Re: Keeping Program Execution Going

  #5  
Oct 24th, 2004
put getch();
just before u before u end your program that is before your curly brackets
Reply With Quote  
Join Date: Sep 2004
Posts: 6,585
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 501
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Keeping Program Execution Going

  #6  
Oct 24th, 2004
>system("pause");
>put getch();
You know what I find funny? The fact that every time this question is asked, the first two replies suggest the first two absolute worst options. :rolleyes: system is slow, nonportable, and a serious security hole and getch is the patron saint of nonportable.

>What can I do to keep the program running using Command Prompt.
The first thing you need to do is start using correct C++. iostream.h is not a C++ header, so either your compiler is frightfully old, or you aren't learning from the right source. Here is the corrected code with one solution for your command line problem:
//
// Program to convert temperature from Celsius degree
// units into Fahrenheit degree units:
// Fahrenheit = Celsius  * (212 - 32)/100 + 12
//
#include <iostream>

using namespace std;

int main()
{
  // enter the temperature in Celsius
  int celsius;
  cout << "Enter the temperature in Celsius: ";
  cin >> celsius;

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

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

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

  // remove extraneous characters from the input stream
  char ch;
  while ( cin.get ( ch ) && ch != '\n' )
    ;

  // pause until the user hits Enter
  cout << "Press Enter to continue...";
  cin.get();
}
Other (cooler) alternatives involve such creations as:
#include <limits>

// remove extraneous characters from the input stream
cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
and
// remove extraneous characters from the input stream
cin.ignore ( cin.rdbuf()->in_avail() );
If none of these solves your problem then either you aren't doing it right, or your problem is different than the common issue of running a program from within an IDE where the IDE opens a new console window and closes it immediately after the program finishes.
I'm here to prove you wrong.
Reply With Quote  
Join Date: Sep 2004
Location: Delaware, USA
Posts: 417
Reputation: Young Teck 06 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Young Teck 06's Avatar
Young Teck 06 Young Teck 06 is offline Offline
Street Game CEO

Re: Keeping Program Execution Going

  #7  
Oct 24th, 2004
I dont think its the book source, the book is great. Possibly the C++ program. I am trying to save up for Microsoft Visual C++ "Standard". Do you think thats a good C++ Compiler?
Need Website Work... PM Me or EMail Me at mdstreetsoulja@gmail.com ... I am AVAILABLE!
Reply With Quote  
Join Date: Sep 2004
Posts: 6,585
Reputation: Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of Narue has much to be proud of 
Rep Power: 31
Solved Threads: 501
Super Moderator
Narue's Avatar
Narue Narue is offline Offline
Expert Meanie

Re: Keeping Program Execution Going

  #8  
Oct 24th, 2004
>I dont think its the book source
Unfortunately, you can only accurately judge the quality of a book if you know the topic it covers intimately. If your book uses <iostream.h> then it's too old to be used as a book to jumpstart your learning.

>the book is great.
I'm getting flashbacks to when beginners were hailing the godawful books of Herbie Schildt because they were easy to understand. *shudder*

>I am trying to save up for Microsoft Visual C++ "Standard".
There are free compilers that are high quality and support the latest C++ standard. Lack of cash is no excuse, I'm afraid.

>Do you think thats a good C++ Compiler?
I use it primarily, and I haven't had any issues with it yet.
I'm here to prove you wrong.
Reply With Quote  
Join Date: Sep 2004
Location: Delaware, USA
Posts: 417
Reputation: Young Teck 06 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Young Teck 06's Avatar
Young Teck 06 Young Teck 06 is offline Offline
Street Game CEO

Re: Keeping Program Execution Going

  #9  
Oct 24th, 2004
Well, I done paid for the C++ book so it doesnt make sense to get another one. Plus, the Accelerated C++ is so-called a great book to learn by, well it was published in the same year as the C++ for Dummies that I have, whats the goodness of it.?
Need Website Work... PM Me or EMail Me at mdstreetsoulja@gmail.com ... I am AVAILABLE!
Reply With Quote  
Join Date: Sep 2004
Location: Delaware, USA
Posts: 417
Reputation: Young Teck 06 is an unknown quantity at this point 
Rep Power: 5
Solved Threads: 0
Young Teck 06's Avatar
Young Teck 06 Young Teck 06 is offline Offline
Street Game CEO

Re: Keeping Program Execution Going

  #10  
Oct 25th, 2004
It worked! Great job
Need Website Work... PM Me or EMail Me at mdstreetsoulja@gmail.com ... I am AVAILABLE!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:53 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC