help adding function to this code

I was going to post this in the beginners forum, but the sticky said to not post things not suitable for beginners.

I've picked up a guide for learning to program in C++, and it has an example code to convert celsius to fahrenheit.

In an effort to grasp the concepts better I want to make this code ask me for the temperature, and then ask if it is fahrenheit or celsius. Depending on my response I want it to do the conversions and spit out the opposite unit. I'll paste the original code, since what I've done doesn't work.

<
#include <cstdio>
#include <cstdlib>
#include <iostream>

using namespace std;

int main(int nNumberofArgs, char* pszArgs[])
{
int celsius;
cout << "Enter the temperature in Celsius:';
cin >> celsius;

int fahrenheit;
fahrenheit = celsius * 9/5 + 32;

cout << "Fahrenheit value is :";
cout << fahrenheit << endl;

system("PAUSE")
return 0;
>

I think I need to use an if/else but I can't figure out how to get the code to look at what I've typed and assign the number to the int celsius or int fahrenheit.

Thanks in advance for the help!
You can use a switch statement.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

//...

char temp = 0;

cout<<"Please enter c for celsius or f for farenheit: ";
cin>>temp;

switch(temp)
{
case 'c':
//do temperture conversion for celsius
break;
case 'f':
//do temperture conversion for farenheit
break;
}

//output the temperature to the user
//hold the window open
return 0;


try to get away from using system("pause"). It is not cross platform, and tends to generate virus warnings.
Last edited on
thank you! I hadn't known about switches! and right, the pause wasn't working for my mac anyways. I think this book was windows focused.
Topic archived. No new replies allowed.