Loop, and display of table for degrees

(The formula for converting a temperature from Fahrenheit to Celsius is
C=5/9(F-32)
Where F is the Fahrenheit temperature and C is the Celsius temperature. Write a function named celsius that accepts a Fahrenheit temperature as an argument. The function should return the converted Celsius temperature.
Demonstrate the function is correct by calling it in a loop that displays a table of the Fahrenheit temperature 0 through 20 and their Celsius temperature equivalents)

this is all I've got, please help.
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
  #include <iostream>
using namespace std;

int main()

{

int celsius, fahrenheit,tempCount;

cout << "How manytemperatures do you want to measure? " << endl;
cin >> tempCount;

celsius = 5 * (fahrenheit - 32) /9;

for (int count = 1; count <= tempCount; count++)
{
  cout << "Enter a temperature in fahrenheit: "<< endl;
  cin >> fahrenheit;

  celsius = 5 * (fahrenheit -32) / 9;

  cout << "Thetemperature in celsius is: " << celsius << endl;
}

return 0;

}
At line 8, change the variables from int to double. Otherwise your results will always be integers which won't be too accurate.

Write a function named celsius that accepts a Fahrenheit temperature as an argument. The function should return the converted Celsius temperature

You need to write this function.
Demonstrate the function is correct by calling it in a loop that displays a table of the Fahrenheit temperature 0 through 20 and their Celsius temperature equivalents)

You need to write this loop.

Notice that the program requires no input from the user. It just calculates the values and prints them out.
can i use..
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;
double celsius(double fahrenheit)
{
double celsius;
celsius = 5 * (fahrenheit - 32) / 9;
return celsius;
}
int main()

{

int tempCount=20;

cout<<setw(20)<<"fahrenheit"<<setw(20)<<"celsius"<<endl;
for (int count = 1; count <= tempCount; count++)
{
cout<<setw(20)<<count<<setw(20)<<celsius(count)<<endl;
}
system("pause");
return 0;

}
closed account (E0p9LyTq)
No need to have a temp variable in your function, you can simply return the results of the calculation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>

double celsius(double fahrenheit);

int main()
{
   double tempCount = 20.0;

   std::cout << std::setw(20) << "Fahrenheit" << std::setw(20) << "Celsius\n";

   for (double count = 0.0; count <= tempCount; count+=1.0)
   {
      std::cout << std::setw(20) << count << std::setw(20) << celsius(count) << '\n';
   }
}


double celsius(double fahrenheit)
{
   return ((5.0 * (fahrenheit - 32.0)) / 9.0);
}


It is not recommended to mix int and double when doing math calculations, even if the int is a constant. That can cause rounding errors.

5 is an int constant, 5.0 is a double constant.
Last edited on
Topic archived. No new replies allowed.