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

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#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;
}
You are close.

1
2
3
4
5
6
//line 15
getDegree ();
getScale();
//They both return a value, but then you do nothing with it.
// Remember that the variables degree and scale
// are only defined within thier respective functions (not in main) 

Try:
1
2
3
float degree = getDegree(); //store the return value of getDegree into degree
//note: Check your getDegree function, it chould return a float right?
char scale = getScale(); // store the return value of getScale into scale 

--------------------------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
//line 18
if (scale == "F")
  {
    convertFtoC(); //you don't pass degree into the function
    // try convertFtoC(degree);
    // also remember to store the return value somewhere
  }
else if (scale == "C")
  {
    convertCtoF(); //same problem here
  }
else
  cout << "ERROR: Invalid temperature scale" << endl;

--------------------------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
//line 77: Check your syntax!
{
  int getResults ()
  cout << "Your temperature reading converts as follows:" << endl;
  cout << "Fahrenheit:" << return Ftemp << endl; //also, you can't return a value here
  cout << "Celsius:" << return Ctemp << endl; //or here
  //maybe this function should be void
  // and Ftemp and Ctemp should be passed as parameters
}
Last edited on
Functions are used to help out main. By helping out it makes main's code much shorter and the code becomes more readable. Functions need to actually accomplish something for them to be useful though. (Otherwise why would anyone program them to begin with?!) There are two types of basic functions in c++: those that return something and those that return nothing. For example in your program the overview function doesn't return anything to main, that is why it has the void type. However the other functions all have return values and would probably used in calculations in main. Line 15 and 16 are a good example of not using functions that return values properly.
1
2
  getDegree ();
  getScale();

Some more useful code might look like:
1
2
  int degree = getDegree(); //creates an integer variable named degree and assigns the value returned from getDegree()
  char scale = getScale(); //creates a character variable named scale and assigns the value returned from getDegree() 

Lines 18, 20, 22, 24, and 77 have errors on them.
Lines 18 and 22: The variables referenced on these lines haven't been declared. (The suggestion I made above for lines 15 and 16 will fix this problem also.)
Lines 20 and 24: Here the conversion functions both expect a float variable to be passed to them. To pass a variable just put the name of it inside the parentheses (hint: convertCtoF(degree); or convertFtoC(degree);)
On line 77: the curly brace is just on the wrong line that's all.

Also on lines 17 and 28 the curly braces aren't really necessary.

Hope I helped let us know if you have more questions about functions. Also you might want to check out the reference page here: http://cplusplus.com/doc/tutorial/functions/
Thank you so much for all of your help! I really appreciate it. This is my updated program. I am still having trouble in lines 80 and 81. I know that I need to put the variable in the parameter, but I do not understand what I need to put 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#include <iostream>
using namespace std;

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

int main ()
{
  cout.precision (2);
  programOverview ();
  float degree = getDegree ();
  char scale = getScale();

  if (scale == 'F')
    {
      convertFtoC(degree);
    }
  else if (scale == 'C')
    {
      convertCtoF(degree);
    }
  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;
  return scale;
}

//This function requires the user to enter the temperature reading in degrees
float getDegree ()
{
  float 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
void getResults ()
  {
  cout << "Your temperature reading converts as follows:" << endl;
  cout << "Fahrenheit:" << Ftemp << endl;
  cout << "Celsius:" << Ctemp << endl;
}
Nevermind, I figured it out! Thank you so much!!!! :)
I'm glad you did. Please ask if you have any more questions.
I actually do have more questions. :/ My setprecision in line 15 is not working correctly, and when I run the program in g++ the conversions are really reallyyy off! Is it because of what I did on lines 81 and 82?

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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <iostream>
#include <cstdlib>
using namespace std;

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

int main ()
{
  programOverview ();
  cout << setprecision (2) << fixed;
  float degree = getDegree ();
  char scale = getScale();
  {
  if (scale == 'F')
    {
      convertFtoC(degree);
    }
  else if (scale == 'C')
    {
      convertCtoF(degree);
    }
  else
    cout << "ERROR: Invalid temperature scale" << endl;
    exit (EXIT_FAILURE);
  }
  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;
  return scale;
}

//This function requires the user to enter the temperature reading in degrees
float getDegree ()
{
  float 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
void getResults ()
{
  int Ftemp;
  int Ctemp;
  cout << "Your temperature reading converts as follows:" << endl;
  cout << "Fahrenheit:" << Ftemp << endl;
  cout << "Celsius:" << Ctemp << endl;
}
You are declaring two variables on lines 80 and 81, but giving them no values. So they have garbage data in them.
1
2
3
4
5
6
7
8
9
//line 79
void getResults (float Ftemp, float Ctemp) //Ftemp and Ctemp should be parameters,
  // that way their values are input.
  // Just remember to pass the temps to the function when you invoke it.
{
  cout << "Your temperature reading converts as follows:" << endl;
  cout << "Fahrenheit:" << Ftemp << endl;
  cout << "Celsius:" << Ctemp << endl;
}
Last edited on
Let me ask you this; what is this program going to do when you run it?
I feel like it should do something like this...
This program will convert a temperature reading provided in
either Fahrenheit or Celsius to the other measurement scale.
Enter your temperature reading in degrees:100
Enter the letter of the temperature scale that will be used
(F = Fahrenheit; C = Celsius):C
Your temperature reading converts as follows:212 F

Am I correct?

If this is the case, your getResults function should take the degrees value (float) and the scale (char),
and print out that last message:
"Your temperature reading converts as follows:212 F" (as an example.)

So let getResults be defined as such (it needs a float and a char, and should return nothing. i.e. void):
1
2
void getResults(float temp, char scale);
//I'll leave the implementation up to you :) 
Last edited on
Yes, the program is supposed to be able to oupput

This program will convert a temperature reading provided in
either Fahrenheit or Celsius to the other measurement scale.
Enter your temperature reading in degrees: 212
Enter the letter of the temperature scale that will be used
(F = Fahrenheit; C = Celsius): F
Your temperature reading converts as follows:

Fahrenheit: 212.00
Celsius: 100.00

I just keep getting a bunch of errors when I try to fix the getResults function
If you need to output both then you need to pass both temps to the function so go back to declaring the function as
 
void getResults(float Ftemp, float Ctemp);


Now you need to pass these values to the function on line 29 when you invoke it.
1
2
//something like
getResults(Ftemp, Ctemp); //if Ftemp and Ctemp have the temps in them... 
Last edited on
When I do that, it says that I didn't delcare Ftemp or Ctemp in the scope..
So now let's look at your main function.
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
int main ()
{
  programOverview ();
  cout << setprecision (2) << fixed;
  float degree = getDegree ();
  char scale = getScale();
  float Ftemp, Ctemp; //we need to define Ftemp and Ctemp
  {
  if (scale == 'F')
    {
      Ctemp = convertFtoC(degree); //we need to store the converted temp into Ctemp
      Ftemp = degree // we also need to store the origonal temp into Ftemp
    }
  else if (scale == 'C')
    {
      //here we do the opposite of above
      Ftemp = convertCtoF(degree); //store the conversion into Ftemp (instead of Ctemp)
      Ctemp = degree; //store origonal into Ctemp (instead of Ftemp)
    }
  else
    cout << "ERROR: Invalid temperature scale" << endl;
    exit (EXIT_FAILURE);
  }
  //now Ftemp and Ctemp have been assigned the proper values
  // you can pass them to the getResults function
  getResults(Ftemp, Ctemp);

  return 0;
}
Okay, I completely understand that now with all of your comments. That was extremely helpful!
No problem.
Feel free to let me know if you have any more questions.
Topic archived. No new replies allowed.