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.

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

I use GNU C++ Compiler, really need to get MS Visual C++ so everybody says its EXCELLENT.

put getch();
just before u before u end your program that is before your curly brackets

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

>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.

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

>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
Are you using Accelerated C++ or C++ for Dummies? If it's the former then your interpretation of the code is wrong because Accelerated C++ never uses <iostream.h>. If it's the latter, I have no comments because I haven't flipped through that book in some time and current editions may be better.

>Well can sumbody please refer me a good UP-TO-DATE C++ book.
I already have. But if it was written after 2003 then it's probably a safe bet that it's up to date.

Alright. I'll look for some, for right now. I dont know what I want to do with this darned C++ for Dummies.

And the Visual C++ 2005 BETA Version, I never received it yet.

This article has been dead for over six months. Start a new discussion instead.