Unitialized memory

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

fahrenheit = (celsius * (9 / 5)) + 32;
kelvin = celsius + 273.15;

for (celsius = startTemp; endTemp + increment >= celsius; celsius += increment)
{
// printf("%5.3f" "%5.3f" "%5.3f" , "celsius, fahrenheit, kelvin");
printf("%5.3f \n", celsius);
printf("%5.3f \n", fahrenheit);
printf("%5.3f \n", kelvin);


}

}
https://www.cplusplus.com/articles/jEywvCM9/
So we have half a chance of figuring out what line 55 might be.
fahrenheit = (celsius * (9 / 5)) + 32;
kelvin = celsius + 273.15;

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.
Last edited on
@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.

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
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
  double start, end, inc;

  cout << "\n\nThis program will make a table converting Celcius to their";
  cout << "Fahrenheit and Kelvin equivalents.\n" << endl;
  cout << "\tPlease enter Start, End & Increment\n\t-> ";  
  cin >> start >> end >> inc;

  if ((start > end) && inc > 0)
    inc *= -1; 
  else 
    inc = abs(inc);

  short iterations = abs ((start - end) / inc) + 1;
  cout << "\n\n";
  
  do {
    cout << fixed << setprecision(2) << setw(12) << start;
    cout << setw (8) << start * (9/5)+32 << "F" << setw (8) << start + 273.15 << "K" << endl;
    start += inc;
  } while (--iterations);
     
  cout << "\n" << endl;
  return EXIT_SUCCESS;
}

This code is not bullet proof, but it does point you in the right direction.
@TightCoder,
Maybe check your results?

What do you think (9/5) evaluates as in c++?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>
using namespace std;

int main() {
	cout << "This program will make a table converting Celcius to their\n";
	cout << "Fahrenheit and Kelvin equivalents.\n\n";
	cout << "Please enter Start, End & Increment: ";

	double start {}, end {}, inc {};
	cin >> start >> end >> inc;

	inc = (start > end) && inc > 0 ? 0 - inc : abs(inc);

	cout << '\n';

	for (auto iterations {abs((start - end) / inc) + 1}; iterations--; start += inc) {
		cout << fixed << setprecision(2) << setw(12) << start;
		cout << setw(8) << start * (9.0 / 5.0) + 32.0 << "F" << setw(8) << start + 273.15 << "K\n";
	}

	cout << '\n';
}



Please enter Start, End & Increment: 50 -50 5

       50.00  122.00F  323.15K
       45.00  113.00F  318.15K
       40.00  104.00F  313.15K
       35.00   95.00F  308.15K
       30.00   86.00F  303.15K
       25.00   77.00F  298.15K
       20.00   68.00F  293.15K
       15.00   59.00F  288.15K
       10.00   50.00F  283.15K
        5.00   41.00F  278.15K
        0.00   32.00F  273.15K
       -5.00   23.00F  268.15K
      -10.00   14.00F  263.15K
      -15.00    5.00F  258.15K
      -20.00   -4.00F  253.15K
      -25.00  -13.00F  248.15K
      -30.00  -22.00F  243.15K
      -35.00  -31.00F  238.15K
      -40.00  -40.00F  233.15K
      -45.00  -49.00F  228.15K
      -50.00  -58.00F  223.15K

@lastchance
What do you think (9/5) evaluates as in c++?
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.
Last edited on
@tightcoder thank you so much, this does help me learn. I couldn't figure out for the life of me how to display it hahaha
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?

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#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 << 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;

	}

}
Last edited on
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.


They really do make it easier. For us and you.
The only thing you do in the loop is write out current values. Move your calculations of fahrenheit and kelvin into (not before) the loop.

1
2
3
4
5
6
7
8
9
10
	fahrenheit = (celsius * 1.8) + 32.0; // 9/5 = 1.8
	kelvin = celsius + 273.15;

		for (celsius = startTemp; endTemp + increment > celsius; celsius += increment)
	{
			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.
> Errors in my equations?
for the third time, they are not equations
@ne555 could you explain a bit deeper?
@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?

1
2
3
4
5
6
7
8
9
10
11
12
13
	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 = (celsius * 1.8) + 32.0; << "F";
		cout << setprecision(3) << setw(5) << kelvin = celsius + 273.15; << "K" << endl;

	}
@dhayden nevermind, i took out the fahrenheit = and the kelvin =
= 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'
Topic archived. No new replies allowed.