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;
}
|