help needed on Easy temperature program.

Doing this program for HW and im really close but i can not figure out what im doing wrong the formula is correct but the numbers are still off.
NEW TO C++ SO ANY FEEDBACK HELPS


Question:
Design a program that displays a table of the Celsius temperatures 0 through 20 and their Fahrenheit equivalents. The formula for converting a temperature from Celsius to Fahrenheit is
F = 95 C + 3 2
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 <string>
using namespace std;
int
main ()
{
  cout << "Temperature translation";
  // Using a while loop print side by side Celsius temperature from 0 to 20

  int c = 0, f;

  while (c <= 20)
    {
      f = (9 / 5) * c + 32;
      cout << " f= " << f;

      cout << "c \n " << c;
      c = c + 1;
    }

  return 0;
}
integer division returns an integer, 9/5 is 1
write 9.0/5.0 instead.
I assume you also want the temperatures you output to have a decimal point? So ontop of making 9 and 5 into decimals, you should also declare "f" as a float/double.
Hello starterpack6100,

I adjusted your program a bit. Give it a run and see what you think.
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>
#include <iomanip>  // <--- Added.
#include <string>

using namespace std;

int main()
{
	cout << "\n     Temperature translation\n\n";  // <--- Changed.
	// Using a while loop print side by side Celsius temperature from 0 to 20

	double c{}, f{};

	std::cout << std::fixed << std::showpoint << std::setprecision(2);  // <--- Added.
	
	while (c <= 20)
	{
		f = (9.0 / 5.0) * c + 32;

		//cout << " f = " << f << "c  " << c;
		std::cout << ' ' << std::setw(5) << c << " degrees C = " << f << " degrees F\n";

		c++;//or c += 0.5;
	}

	return 0;
}


Any questions let me know.

Hope that helps,

Andy
Thank you so much Andy i have been struggling with this program for like 4 days.
Do you suggest i use only decimals when coding programs like this ? i was using integers, i think that made it harder to get an exact number.
@startpack6100, not just harder, impossible.

So yes, use floats or doubles when doing this kind of work.
Hello starterpack6100,

You are welcome.

What you are trying to accomplish in a program will determine what type of variable to use.

Integers only work with whole numbers where as "double"s and "float"s, "float"s are less precise than "doubles" and not used as much these days, work with floating point numbers.

What you need for a calculation or for output helps determine the type of variable. In this case when you put a floating point number into an int you only keep the whole number and loose the decimal part. Also dividing by (9 / 5) as an integer results in "1" as ne555 has pointed out. By dividing (9.0 / 5.0) the result is "1.8" which will make a big difference in your calculation.

By defining "c" and "f" as a "double" it is a way to avoid any data loss when storing the result of a calculation (what would be a double) into an "int".

To save time of having to type case everything as a "double" you start by defining the variables as "double"s and use "9.0 and 5.0" in the calculation. Technically speaking only one of those numbers needs to be a "double" and the other will be promoted to a "double" for the calculation.

Another way to think of this is that anything on the rhs of the "=" needs to be a "double" or used in a way that the compiler will promote the variable to a "double".

BTW while I am thinking about it using "c" and "f" for variable names is not a problem with code this short, but with a larger amount of code that could span two or more screens variable names of "celsius" and "ferenheit"make the code easier to follow. Also you should avoid single letter variable names.

As Repeater once wrote:

You know you're not being charged for every letter you use, right? If you want people to be able to read your code (which you do), use variable names that help people understand what you're trying to do.



Hope that helps,

Andy
Topic archived. No new replies allowed.