the following code is to a program that will layout a table that will convert celsius to fahrenheit and kelvin. my issue lies on line 55 where the compiler tells me celsius is uninitialized but i'm not sure why. does it have anything to do with where i have my conversion operations?
also any other errors you spot would help me greatly if you could provide feedback!
code:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//This program is design to convert temperatures from celsius
//and display them in a chart at certain intervals
double startTemp; // user defined
double endTemp; // user defined
double celsius;
double kelvin;
double fahrenheit;
double increment; // user defined
cout << "This program will make a table converting Celcius temperatures into their Faranheit and Kelvin equivalents." << endl;
cout << "\n\n";
cout << "What Celcius temperature would you like to begin the table with?" << endl;
cin >> startTemp;
cout << "\n";
cout << "What ending Celcius temperature would you like?" << endl;
cin >> endTemp;
cout << "\n";
cout << "And at what increment would you like to increase by?" << endl;
cin >> increment;
cout << "\n\n";
cout << "C F K" << endl;
// startTemp = celsius means that the celsius to be converted is the starting value given by user
// endTemp + increment >= celsius means that the converted temperature must be less than equal to the desired
// ending temperature in order to exit the loop and complete the table
// celsius += increment means the desired increment will add to the starting temperature each time
// the loop updates until it reaches the maximum temperature
what value do YOU think celsius has here? You use it, but you never got it via cin and you did not initialize it or anything.
also why use printf? Do you know how to print formatted c++ doubles? If you do and its just handy, that is fine for a short program, if you don't, review how to format with c++ tools.
@jonnin I have it to where the input for startTemp would equal celsius once inputted
and I'm not sure why my teacher chose printf, I just know he wants the decimal places in each column to be lined up equally. that's all we've cover in formatting besides setw()
Not generally a good idea to do someone's homework for them, but at least if you study the code so you can explain why it works the way it does, would be at least something. You did however present a reasonable effort. Here is an example of starting @ 50 to -50 in increments of 5.
This program will make a table converting Celcius to theirFahrenheit and Kelvin equivalents.
Please enter Start, End & Increment
-> 50 -50 5
50.00 82.00F 323.15K
45.00 77.00F 318.15K
40.00 72.00F 313.15K
35.00 67.00F 308.15K
30.00 62.00F 303.15K
25.00 57.00F 298.15K
20.00 52.00F 293.15K
15.00 47.00F 288.15K
10.00 42.00F 283.15K
5.00 37.00F 278.15K
0.00 32.00F 273.15K
-5.00 27.00F 268.15K
-10.00 22.00F 263.15K
-15.00 17.00F 258.15K
-20.00 12.00F 253.15K
-25.00 7.00F 248.15K
-30.00 2.00F 243.15K
-35.00 -3.00F 238.15K
-40.00 -8.00F 233.15K
-45.00 -13.00F 228.15K
-50.00 -18.00F 223.15K
Experiment with this code like -50.25 75.5 -1.5 and see what happens.
I was kind of hoping OP would have picked up on that first as -40C -8F does kind of stick out and (9/5) it what he had in his code, even though his version doesn't display anything.
@seeplus
Interesting too is that your version optimized with -O3 comes in at almost 400 bytes smaller than mine.
@OP: fahrenheit = (celsius * (9 / 5)) + 32;
that's not an equation but an assignment
it takes the current value stored on the `celsius' variable perform those operations and set the `fahrenheit' variable to that result.
so, at that point, ¿what's the value of `celsius'?
as you never change `fahrenheit' later, it will still have the same value in the loop. that you change `celsius' is irrelevant, again, it is not an equation.
also, integer division returns an integer, so 9/5 is 1
oh ... you thought that you were making a function with that 'equation'.
that would look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13
double fahrenheit (double C)
{
return (C * 1.8) + 32; //meh 9.0/5.0 = 1.8 which is easier to write.
}
...
main()
{
...
for (celsius = startTemp; endTemp + increment >= celsius; celsius += increment)
{
printf("%1.10f", fahrenheit(celsius)); //uses the returned value of the function
}
}
its just as easy to to it inline, but that is how you would do it if you had a more complex equation or had to do it over and over in many places.
So I've pasted the updated code with a better output format and the errors fixed, however When I input startTemp as 0, endTemp as 100, and increment as 10, the conversions don't take place, it only outputs 32F and 273.15K over and over.
Any reason why? Faulty input? Errors in my equations?
#include <iostream>
#include <iomanip>
usingnamespace std;
int main()
{
//This program is design to convert temperatures from celsius
//and display them in a chart at certain intervals
double startTemp; // user defined
double endTemp; // user defined
double celsius;
double kelvin;
double fahrenheit;
double increment; // user defined
cout << "This program will make a table converting Celcius temperatures into their Faranheit and Kelvin equivalents." << endl;
cout << "\n\n";
cout << "What Celcius temperature would you like to begin the table with?" << endl;
cin >> startTemp;
cout << "\n";
cout << "What ending Celcius temperature would you like?" << endl;
cin >> endTemp;
cout << "\n";
cout << "And at what increment would you like to increase by?" << endl;
cin >> increment;
cout << "\n\n";
cout << setw(5) << "C";
cout << setw(7) << "F";
cout << setw(6) << "K" << endl;
cout << setw(4) << "-----------------" << endl;
// startTemp = celcius means that the celcius to be converted is the starting value given by user
// endTemp + increment >= celcius means that the converted temperature must be less than equal to the desired
// ending temperature in order to exit the loop and complete the table
// celcius += increment means the desired increment will add to the starting temperature each time
// the loop updates until it reaches the maximum temperature
celsius = startTemp;
fahrenheit = (celsius * 1.8) + 32.0; // 9/5 = 1.8
kelvin = celsius + 273.15;
for (celsius = startTemp; endTemp + increment > celsius; celsius += increment)
{
// printf("%5.3f" "%5.3f" "%5.3f" , "celsius, fahrenheit, kelvin"); is a bit too messy for me, also a tad confusing
cout << setprecision(3) << setw(5) << celsius << "C";
cout << setprecision(3) << setw(5) << fahrenheit << "F";
cout << setprecision(3) << setw(5) << kelvin << "K" << endl;
}
}
C++ programs don't work like spreadsheets. So when you set fahrenheit at line 1, it doesn't say "whenever I write "fahrenheit, I want you to do this calculation." Instead it says "do this calculation now, assign the resulting numeric value to fahrenheit and then move on." Put another way, when you assign a value to a variable, it retains that specific value until you change it.
So you have to recalculate farenheit (and kelvin) each time though the loop.
@dhayden so when i put the calculations inside the loop, the = and the << of the fahrenheit and kelvin statements have red lines underneath? what's making that error pop up?
= is assignment
when you do fahrenheit = (celsius * 1.8) + 32.0; it takes the value stored in the `celsius' variable to perform the computation
for example, say that `celsius' have a 42, then the program does fahrenheit = (42 * 1.8) + 32.0;
there is no connection created between the variables, if you later modify `celsius', fahrenheit will not change.
¿do you understand the expression foo = foo + 1?
that works because it is an assignment, not an equation that you need to solve for `foo'