# Thread: Temperature Conversion Program

1. ## Temperature Conversion Program

I'm a beginner student learning about functions and do not understand them fully. My assignment was to write a program that deals with temperature conversion. My program had to use 6 functions (1. Program overview 2. Enter temp 3. Enter scale 4. Convert F to C 5. Convert C to F 6. Display results) It also needs to display an error message if an inappropriate input is entered for both scale (which I have) and temperature (which I do not know where to place). Appropriate values for temps are >= -459.67 F or -273.15 C. I do not know where to place this scale either. I also have multiple errors in my program. If anyone could give me any guidance in helping me to fix my program, I would greatly appreciate it!!

Code:
```#include <iostream>
using namespace std;

void programOverview ();
char getScale ();
int getDegree ();
float convertFtoC (float);
float convertCtoF (float);
int getResults ();

int main ()
{
cout.precision (2);
programOverview ();
getDegree ();
getScale();
{
if (scale == "F")
{
convertFtoC();
}
else if (scale == "C")
{
convertCtoF();
}
else
cout << "ERROR: Invalid temperature scale" << endl;
}
getResults();

return 0;
}

//This function displays a brief overview explaining the program to the user
void programOverview ()
{
cout << "This program will convert a temperature reading provided in" << endl;
cout << "either Fahrenheit or Celsius to the other measurement scale." << endl;
}

//This function requires the user to enter the temperature scale to be used
char getScale ()
{
char scale;
cout << "Enter the letter of the temperature scale that will be used:" << endl;
cout << "F = Fahrenheit; C = Celsius)" << endl;
cin >> scale >> endl;
return scale;
}

//This function requires the user to enter the temperature reading in degrees
int getDegree ()
{
int degree;
cout << "Enter your temperature reading in degrees:" << endl;
cin >> degree;
return degree;
}

//This function converts a Fahrenheit temperature to Celsius
float convertFtoC (float Ftemp)
{
float Ctemp;
Ctemp = (Ftemp - 32) / 1.8;
return Ctemp;
}

//This function converts a Celsius temperature to Fahrenheit
float convertCtoF (float Ctemp)
{
float Ftemp;
Ftemp = 1.8 * Ctemp + 32;
return Ftemp;
}

//This function displays the results
{
int getResults ()
cout << "Your temperature reading converts as follows:" << endl;
cout << "Fahrenheit:" << return Ftemp << endl;
cout << "Celsius:" << return Ctemp << endl;
}```

2. 1. getDegree() returns an integer but you aren't storing it anywhere. Not an error but it's against the rules.
2. Same is the case for getScale().
3. convertFtoC (float) is returning and accepting parameters, while you are not providing it.
4. Same is the case with convertCtoF (float);

3. Code:
`tempConversion.cpp:18:7: error: ‘scale’ was not declared in this scope`
You call getScale() but don't store the result in a variable called "scale" that you use on this line:
Code:
`  if (scale == "F")`
to fix it put this in:
Code:
`  char scale = getScale();`
I see that this is a misunderstanding of variable scope which is understandable since you're just now learning about functions. What you have to understand is that that "scale" variable that you create inside the getScale() function only exists while getScale() executes, and furthermore it is only recognised inside getScale(). To get that char value outside, you make that char the return value so you can assign it to a new "scale" variable inside main().

Originally Posted by Mr.777
1. getDegree() returns an integer but you aren't storing it anywhere. Not an error but it's against the rules.
Not really. There are many cases where it is appropriate to ignore a return value. Usually this is because you called the function for the side effect it produces. The return value gives you extra information/functionality but you choose not to use it. E.g.:

Code:
```std::cout << "Hello";
std::cout.flush();```
std:stream::flush() actually returns an ostream&, but it's often not used:
ostream::flush - C++ Reference

Popular pages Recent additions