Assistance needed

Hey guys, im writing my first C++ program, and its working great(after some issues getting used to new ANSI rules, my books a bit outdated, have newer resources now), except for the fact that it automatically closes out. Its a simple program that converts celsius to fahrenheit(yea stupid i know, but i just started yesterday and ya gotta walk before you can crawl), anyways, heres the source code, was just wondering what i was missing on the end to make it stay up and display the results before immediately closing out, thanks for the help guys, and i look forward to being here and learning as much as possible!

//
// Program to convert temperature from Celsius degree
// units into Fahrenheit degree units:
// Fahrenheit = Celsius * (212-32)/100+32
//
#include <stdio.h>
#include <iostream>
;int main(int nNumberofArgs, char* pszArgs[])
{
//enter the temperature in Celsius
;int nCelsius;
std::cout << "Enter the temperature in Celsius:";
std::cin >> nCelsius;

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

//use conversion factor to convert Celsius
// into Fahrenheit values
;int nFahrenheit;
nFahrenheit = nFactor*nCelsius/100+32;

//output the results
std::cout <<"Fahrenheit value is:";
std::cout <<nFahrenheit;

return 0;
}
just for testing put

system("pause");


right before return 0;
immmmmm dumb, lol, thank you
*sigh*
Console Closing Down http://www.cplusplus.com/forum/beginner/1988/
closed account (4Gb4jE8b)
we've had this convo many a time before acorn, everywhere on the forum. DO NOT USE SYSTEM(ANYTHING) EVER!!!!.

*note, my bad i didn't see you said, "for testing" but again, only use it for testing

my personal favorite is this

void pause()
{
cout << "Press ENTER to continue\n";
cin.get();
}

as a function declared before int main(), then call pause() wherever necessary and boom, problem solved
Last edited on
no luck with the void pause gargoyle, im using visual C++ 2010 express atm, maybe an unrecognized command or am i just entering the code wrong?(entered it exactly as you wrote it there, maybe missing a ; before the void?)
@rjd82AA

im new to this too, but my experiences tell me that if you are using windows as the host OS, system("PAUSE") is a pretty easy way to do it. its a system call, so it can be a potential security risk, plus it takes unnecessary resources to execute, all-in-all it sucks and isnt a good idea. but for sheer simplicity, its easy to tack on at the end of your source code just for the purpose of testing.

plus system calls are not platform independent (system(anything) will not work the same on other host OS)

to use headlessgargs way of doing it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
using namespace std;


// ............................................



void pause()
{
cout << "Press ENTER to continue\n";
cin.get();
}



// ......................... ^do that^



// ......................... then the rest of your program

int addition (int a, int b)
{
  int r;
  r=a+b;
  return (r);
}

int main ()
{
  int z;
  z = addition (5,3);
  cout << "The result is " << z;
  return 0;

 // ............. 

 pause()

//  ^ then this ^


}
using namespace std; after the includes is a useful line to add to just about any beginner's code. Eventually you get tired of writing std::cin.get, or std::cout...

I should also add that this site has a great tutorial, if you haven't seen it yet.
Last edited on
Topic archived. No new replies allowed.