Temperature Conversion Program

Hi all beginner hear and im having trouble with the program im writing, below is my code.

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
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>

using namespace std;

void description();
char temperatureScale(char A);
double temperatureReading(double &num);
void convertCtoF();
void convertFtoC();
void Results();

char tempChoice;
double temperature;
float Ftemp, Ctemp, resultF, resultC;

int main()
{

  description();

  char choice;
  choice = temperatureScale(choice);

  double num;
  temperatureReading(num);

  convertFtoC();
  convertCtoF();

  Results();
}

void description()
{
  cout<<"This program will convert a temperature reading provided in " <<endl;
  cout<<"either Fahrenheit or Celcius to the other measurment scale. " <<endl;
  cout<<"                                                            " <<endl;
}

char temperatureScale(char A)
{
  char tempChoice;
  cout<<"What temperature scale did you use (F = Fahrenheit; C = Celsius)? ";
  cin>>tempChoice;
}

double temperatureReading(double &num)
{
  cout<<"Please enter your temperature reading (in degrees): ";
  cin>>temperature;
}

void convertFtoC()
{
  float Ctemp;
  Ctemp = (temperature - 32)/1.8;
}

void convertCtoF()
{
  float Ftemp;
  Ftemp = 1.8 * temperature +32;
  resultF = Ftemp;
}

void Results()
{
      cout<<"Your temperature reading converts as follows: " <<endl;
      cout<<"                                                "<<endl;
      cout<<" Fahrenheit: " <<resultF <<endl;
      cout<<" Celsius: " <<resultC <<endl;
}



It needs to output like this:

Your temperature reading converts as follows:

Fahrenheit: (based on what scale the user selects)
Celcius: (based on the scale the user selects)
------
If the user selects Fahrenheit then the value they enter should just get put in for the results and the only calculation you will get is Celsius and vice versa for the Celsius data.

When I run my program i get this:

This program will convert a temperature reading provided in
either Fahrenheit or Celcius to the other measurment scale.

What temperature scale did you use (F = Fahrenheit; C = Celsius)? F
Please enter your temperature reading (in degrees): 212
Your temperature reading converts as follows:

Fahrenheit: 413.6
Celsius: 100


Please help as you can see if i use Fahrenheit my calculation is right but my celsius isnt and vce versa for inputing C

Thanks.
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
#include <iostream>
using namespace std;
void description();
void temperatureScale(char &);
void convertCtoF();
void convertFtoC();

int main()
{
description();
char choice;
temperatureScale(choice);
if (choice=='c' || temp=='C')
  convertCtoF();
else
  convertFtoC();

return 0;
}

void description()
{
  cout<<"This program will convert a temperature reading provided in " <<endl;
  cout<<"either Fahrenheit or Celcius to the other measurment scale. " <<endl;
  cout<<"                                                            " <<endl;
}
void temperatureScale(char &A)
{
  cout<<"What temperature scale did you use (F = Fahrenheit; C = Celsius)? ";
  cin>>A;
}
void convertFtoC();
{
//declare your temp, ask for the value in celsius and compute this, display your result from here
//no extra function is needed.
}
void convertCtoF();
{
//declare your temp, ask for the value in Fahrenheit and compute this, display your result from here
//no extra function is needed too.
}
Last edited on
is this what you mean?

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
void convertFtoC()
{
   float Ctemp;
  Ctemp = (temperature - 32)/1.8;
  resultC = Ctemp;
  if (tempChoice == F)
    {
    cout<<"Your temperature is converted as follows: " <<endl;
    cout<<"Fahrenheit: " <<temperature <<endl;
    cout<<"Celsius: " <<resultC <<endl;
    }
}

void convertCtoF()
{
  float Ftemp;
  Ftemp = 1.8 * temperature +32;
  resultF = Ftemp;
  if (tempChoice == C)
    {
      cout<<"Your temperature is converted as follows: " <<endl;
      cout<<"Fahrenheit: " <<resultF <<endl;
      cout<<"Celsius: " <<temperature <<endl;
    }
}


I get a wierd error when i do it that way, I need both the Fahrenheit and Celsius to output at the same time.
Where is the value of temperature in line 4 and 17? Whats with line 5, 18 anyway.
edit
my bad, i didn't know you want both at the same time. Accept the temperature from the user, send it to both functions convertFtoC and convertCtoF from main and now you can exclude the if part. i.e.
1
2
3
4
5
6
7
8
convertCtoF(double temperature)
{
//yourcode here
}
convertFtoC(double temperature)
{
//yourcode here
}
Last edited on
I am almost there, thanks for all your help so far.

When i run my code I get this:

This program will convert a temperature reading provided in
either Fahrenheit or Celcius to the other measurment scale.

Please enter your temperature reading (in degrees): 37
What temperature scale did you use (F = Fahrenheit; C = Celsius)? c
Your temperature is converted as follows:
Fahrenheit: -2.24605e+307
Celsius: -Inf


Why the weird calculations?
temperature is what the user enters, just to answer your last question better
Can you show me all your code? I need to know where we're flying blind.
Last edited on
Here is is from line 1

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
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>

using namespace std;

void description();
char temperatureScale(char &);
double temperatureReading(double num);
void convertCtoF(double temperature);
void convertFtoC(double temperature);
void Results();

char tempChoice;
double temperature, num;
float Ftemp, Ctemp, resultF, resultC;

int main()
{

  description();

  char choice;
  choice = temperatureScale(choice);
  if (choice == 'c' || tempChoice == 'C')
    {
      temperatureReading(num);
    double temperature;
  convertCtoF(temperature);
    }
  else
    {
     temperatureReading(num);
    double temperature;
  convertFtoC(temperature);
    }
  return 0;
}

void description()
{
  cout<<"This program will convert a temperature reading provided in " <<endl;
  cout<<"either Fahrenheit or Celcius to the other measurment scale. " <<endl;
  cout<<"                                                            " <<endl;
}

char temperatureScale(char &A)
{
  char tempChoice;
  cout<<"What temperature scale did you use (F = Fahrenheit; C = Celsius)? ";
  cin>>A;
}

double temperatureReading(double num)
{
  cout<<"Please enter your temperature reading (in degrees): ";
  cin>>temperature;
}

void convertFtoC(double temperature)
{
  float Ctemp;
  Ctemp = (temperature - 32)/1.8;
      cout<<"                                          " <<endl;
      cout<<"Your temperature is converted as follows: " <<endl;
      cout<<"Fahrenheit: " <<temperature <<endl;
      cout<<"Celsius: " <<Ctemp <<endl;
}

void convertCtoF(double temperature)
{
  float Ftemp;
  Ftemp = 1.8 * temperature +32;
      cout<<"                                          " <<endl;
      cout<<"Your temperature is converted as follows: " <<endl;
      cout<<"Fahrenheit: " <<Ftemp <<endl;
      cout<<"Celsius: " <<temperature <<endl;
}


And the result of that code are the weird values i showed before
You're complicating things for yourself.

On line 9, you don't need to have temperatureScale as char, just leave it as void temperatureScale(char &);[/code] and on line 25, you don't need to assign temperatureScale(choice) to choice i.e. choice=temperatureScale(choice), just say temperatureScale(scale).
You don't need line 26 to 37 anymore, just replace them with convertCtoF(temperature) and convertFtoC(temperature)
i.e.
1
2
3
4
5
6
7
8
//after temperatureScale(choice);
//just do
double temperature;
temperatureReading(temperature);
convertCtoF(temperature);
convertFtoC(temperature);
return 0;
}

whereas you have
1
2
3
4
5
void temperatureReading(double &num)
{
cout<<"Enter the temperature: ";
cin>>num;
}
I replaced everything as you suggested and this is what my code now looks like:

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
#include <iostream>
#include <cmath>
#include <iomanip>
#include <string>

using namespace std;

void temperatureScale(char &);
double temperatureReading(double num);
void convertCtoF(double temperature);
void convertFtoC(double temperature);
void description();

char tempChoice;
double temperature, num;
float Ftemp, Ctemp, resultF, resultC;

int main()
{

  description();

  char choice;
  temperatureScale(choice);
  double temperature;
  temperatureReading(temperature);
  convertCtoF(temperature);
  convertFtoC(temperature);
  return 0;
}
void description()
{
  cout<<"This program will convert a temperature reading provided in " <<endl;
  cout<<"either Fahrenheit or Celcius to the other measurment scale. " <<endl;
  cout<<"                                                            " <<endl;
}

void temperatureScale(char &A)
{
  char tempChoice;
  cout<<"What temperature scale did you use (F = Fahrenheit; C = Celsius)? ";
}

double temperatureReading(double num)
{
  cout<<"Please enter your temperature reading (in degrees): ";
  cin>>temperature;
}

void convertFtoC(double temperature)
{
   float Ctemp;
  Ctemp = (temperature - 32)/1.8;
      cout<<"                                          " <<endl;
      cout<<"Your temperature is converted as follows: " <<endl;
      cout<<"Fahrenheit: " <<setprecision(2) <<temperature <<endl;
      cout<<"Celsius: " <<setprecision(2) <<Ctemp <<endl;
}

void convertCtoF(double temperature)
{
  float Ftemp;
  Ftemp = 1.8 * temperature +32;
      cout<<"                                          " <<endl;
      cout<<"Your temperature is converted as follows: " <<endl;
      cout<<"Fahrenheit: "<<setprecision(2) <<Ftemp <<endl;
      cout<<"Celsius: " <<setprecision(2) <<temperature <<endl;
}



And now when I output is both convertFtoC and convertCtoF show the cout lines....hmm...
OKAY! let's finish this once and for all
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
#include <iostream>
#include <iomanip>
using namespace std;

void temperatureReading(double &);
void convertCtoF(double temperature);
void convertFtoC(double temperature);
void description();

int main()
{
  double temperature;
  description();
  temperatureReading(temperature);
  convertCtoF(temperature);
  convertFtoC(temperature);
  return 0;
}
void description()
{
  cout<<"This program will convert a temperature reading provided in " <<endl;
  cout<<"either Fahrenheit or Celcius to the other measurment scale. " <<endl;
  cout<<"                                                            " <<endl;
}

void temperatureReading(double &num)
{
  cout<<"Please enter your temperature reading (in degrees): ";
  cin>>num;
}

void convertFtoC(double temperature)
{
      float Ctemp;
      Ctemp = (temperature - 32)/1.8;
      cout<<"Fahrenheit: " <<setprecision(2) <<Ctemp <<"F"<<endl;
      }

void convertCtoF(double temperature)
{
  float Ftemp;
  Ftemp = 1.8 * temperature +32;
      cout<<"                                          " <<endl;
      cout<<"Your temperature is converted as follows: " <<endl;
      cout<<"Celsius: " <<setprecision(2) <<Ftemp <<"C"<<endl;
}



You can find my sample input here: http://codepad.org/IRJqQsTW
I had to change line 29 because the compiler on the website cannot make use of cin, so I assumed the user entered 20.
Last edited on
I'm afraid I am still receiving unorthodox answers to both calculated values my code is very very similar to the code you just supplied me with,
The code works, but I get a weird value for an answer and i am unsure why, anyone have any suggestions?
Topic archived. No new replies allowed.