Temperature conversion program.

I need help again guys. I am working on a program to convert temperatures. This is what we are supposed to do.

The program needs to convert Fahrenheit to Centigrade, show both sets of temperatures in tabular form, and get the average of both sets of temperatures. Here's what I have so far.
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
#include <iostream>
using namespace std;


int main()

{

float F;

float C;

sumf=0, sumc=  0;

int count = 0;

cout << "Enter Fahrenheit temperature to continue. Enter -9999999 to stop.: ";

while(f!= 9999999)
     {

Count++;

cin >> F;

C= (5.0/9.0) * (F-32)

out.setf(ios::fixed);

Cout.setf(ios::showpoint);

Cout.precision(1);

cout<<”Ft = “<<Fahrenheit<< setw=10

Cout<<”C = “<<Centigrade<< setw=23

<<”avg fahrenheit =”<<sumf/count<<;
<<”avg centigrade =”<<sumc/count<<;
return 0;


I know it's a bit messy, but I wanted to leave room inbetween the lines for commments.
Lots of things to straighten out here.
There is no closing brace for the while loop, nor for function main.
Mixture of uppercase and lowercase text. C++ is case sensitive.
Line 13, variables are used but have not yet previously been defined.
around lines 34-39 missing or misplaced semicolon.
setw() is misused.

Forget the blank lines, indentation is more important.
Last edited on
This is my updated version. I think I am still missing stuff. BUt here.

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
#include <iostream>
using namespace std;


int main()

{

float F;

float C;

sumf=0, sumc=  0;

int count = 0;

cout << "Enter Fahrenheit temperature to continue. Enter -9999999 to stop.: ";

while(f!= 9999999)
     {

count++;

cin >> F;

c= (5.0/9.0) * (F-32)

out.setf(ios::fixed);

cout.setf(ios::showpoint);

cout.precision(1);

cout<<”Ft = “<<Fahrenheit<< setw()=10

Cout<<”C = “<<Centigrade<< setw()=23

<<”avg fahrenheit =”<<sumf/count<<;
<<”avg centigrade =”<<sumc/count<<;
return 0;}
closed account (17ihb7Xj)
Take another look at Chervil's post. You have two variables in line 13, but have not declared them. When declaring variables, you need to define them with a type, such as float, int, char, etc. I can also see that you want to use sumf and sumc for getting the average temperatures. However, the only thing you do with these variables (while they are not declared) is divide them by count. You are not setting those values to anything, so your average temperatures are always going to be 0, unless you write a section of code that will keep track of the sum of the values.

Something like this may work
1
2
3
4
5
6
7
//Declare the variables
float sumf, sumc = 0;

//Do some stuff here in code

sumc += C
sumf += F


You will also need to close that while loop and main function like Chervil pointed out. I would suggest using a do{} while() loop, to ensure that you're code gets executed at least once.

You are using the function setw() incorrectly as well.

Be careful with the names of your variables, C++ is case sensitive. It will also help you, and others, understand your code if you were to use more descriptive variables. For example sumc could be renamed to centigradeSum, sumf to fahrenheitSum, etc.

And I agree with Chervil, try to eliminate blank lines, and instead use the tab key to indent lines of code
This is what I have done so far. I guess I'm just dumb when it comes to this stuff, I'm just not getting 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
#include <iostream>
#include <iomanip>
using namespace std;
int main()
  {
    float f;
    float c;
   float sumf=0, sumc=0;
    int count =0;
    cout << "Enter Fahrenheit temperature to continue. Enter -9999999 to stop. ";
      while(f!= 9999999)    
      {count++;
      cin >> f;
      c= (5.0/9.0) * (f-32);
   }
    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(1);
    cout<<”f = “<<Fahrenheit<< setw(10);
    Cout<<”C = “<<Centigrade<< setw(23);
  <<”avg fahrenheit =”<<sumf/count<<setw(10);
  <<”avg centigrade =”<<sumc/count<<(23):
 system("pause")
return 0;}
}
Don't forget, the compiler is there to help you.
Currently, the code posted above doesn't compile, the messages from the compiler can be used to guide you as to what is wrong, at least in terms of valid code.

I'm guessing you wrote this in a word-processor, not a text editor, as there are so-called "smart quotes" in there on lines 19 and 20. So my first advice is to use a plain text editor, preferably one designed for writing code, such as notepad++, or the editor built into the IDE.

As for the code, this loop is flawed in several ways:
1
2
3
4
5
6
while (f!= 9999999)    
{
    count++;
    cin >> f;
    c = (5.0/9.0) * (f-32);
}

For one thing, the variable f is not given any initial value, it will contain some random value, and there's a small chance that the value could be exactly 9999999 and the loop would not execute at all.

In a similar way, when the user inputs the value here cin >> f;, there is no check to see whether the user has entered 9999999. As well as that, c is calculated, but nothing is done with the result, for example print it out or store it somewhere more permanent. Next time around the loop the value will be lost.

However, if we view the original question at a higher level, then the requirement is to print out a table of the F and C values. I think the simplest way to achieve that is to store the user input in an array or vector, until the sentinel value is entered.

After that, loop through the array and print out the table, and accumulate totals. Finally, use the totals to calculate and print the average.

Here's a suggested version, it's more or less an outline, needs some details to be filled in:
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>
#include <iomanip>
#include <vector>

using namespace std;

int main()
{
    vector <float> fahrenheit;

    cout << "Enter Fahrenheit temperature to continue. Enter 9999999 to stop. ";

    float f = 0;

    cin >> f;
    while (f != 9999999)
    {
        fahrenheit.push_back(f);
        cin >> f;
    }

    cout << "Fahrenheit    Centigrade" << endl;

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(1);

    for (int i=0; i<fahrenheit.size(); i++)
    {
        cout << setw(7) << fahrenheit[i];
        // convert f to c here
        float c = 0;
        cout << setw(13) << c << endl;

        // accumulate totals here
    }

    // calculate averages and output results here

    system("pause");
    return 0;
}

Last edited on
Topic archived. No new replies allowed.