Working on a school problem for celsius to fahrenheit requiring that I make a function that contains a for loop that will convert the two temperatures. I also need to create a table that lists the data from 0-20. I have my table created, and my for loop. I believe my calculation is correct but whenever the program is run I get 20 rows of 0s. I know the use of a function is pointless here but it is requested by my teacher. I’m thinking I have to do something to calculation to get the negative numbers to display. While this is basic keep in mind this is only my 3rd month of learning this language so constructive criticism on my code is welcome if you have a seemingly useful answer to the problem.
Edit: screwed everything up in creating this thread. Should be fixed now, forgive me its early.
[code=cpp]
/*
Program Name: Chapter5Program2
Chapter: Chapter 5
Pg# / Ex.: Pg. 201 Program 2
Programmer: Zach Marshall
Date: 3/23/2016
Description: Convert Fahrenheit to Celsius from 0 - 20 degrees by displaying a table. The formula for converting fahrenheit
to celsius is C = 5/9(F-32)
*/
#include
#include
using namespace std;
//function prototypes
double Celsius(int F);
int main()
{
//declare variables
double C;
int F = 0;
//sets decimal place to 2
cout << fixed << setprecision(2);
//setup output table
cout << "Fahrenheit\t" << "Celsius" << endl;
cout << "----------\t" << "-------" << endl;
//define C -- Celsius
C = Celsius(F);
cout << endl; //blank line
return 0;
}
double Celsius(int F)
{
double C;
double multiplier = 5 / 9;
for (F = 0; F <= 20; F++)
{
//conversion
C = multiplier * (F - 32);
//displays the temperatures
cout << F << "\t" << C << endl;
}
return C;
}[/code]
EDIT: Problem solved, teacher pointed out to me that it was a mistake that would smack you in the face.
must be changed to
because 5 / 9 is integer division and can’t be transformed into a double.