laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Hey everyone, I am very confused on why my program is not working. It is suppose to run through a loop give a Fahrenheit Temp 0-20 and displaying the Celsius Temp next to each one. I am suppose to call the Celsius value from a function. Here is what i have:


Code:
#include <iostream>
using namespace std;


int celsius(int);

int main()
{

	int c;

	for (int x = 0; x <= 20; x++)
	{
		c = celsius(x);
		cout << "The Temperature in Fahrenheit is " << x << "	   The Temperature in Celsius is " << c << endl;
	}

	return 0;
}

int celsius(int x)
{
	return ((5/9)(x - 32));
}

What i get is the Fahrenheit temp going from 0-20 but the Celsius is always 0? Everything looks correct after look at similar examples from my book.
 
Solution
This works:

Code:
#include <iostream>
using namespace std;

double celsius(int x);

int main()
{

	double c;

	for (int x = 0; x <= 20; x++)
	{
		c = celsius(x);
		cout << "The Temperature in Fahrenheit is " << x << "	   The Temperature in Celsius is " << c << endl;
	}
	system("pause");
	return 0;
}

double celsius(int x)
{
	double total;
	total = ((5.0/9.0)*(x - 32.0));
	return total;
}

Bruceification73

Distinguished
Oct 5, 2009
169
0
18,660
need to use double instead of int, and change all of the values to whatever.0

Also last part should be ((5.0/9.0)*(x-32.0));

And actually it doesn't really work at all the way you have it set up. Try this:
Code:
#include  <iostream>
using namespace std;

int main()
{
	double x , c;
	for(x=0;x<21;x++)
	{
	c = (5.0 / 9.0) * (x - 32.0);
	cout<<"The temperature in fahrenheit is: "<<x<<"\tThe temperature in celsius is: "<<c<<endl;
	}
	system("pause");
	return 0;
}

This is working for me.
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Thanks, I cant use higher values, its a HW assignment and has to be 0-20, and I need to use functions. but will try some of the stuff you said. I have written other programs but more complicated functions and they work but this one is owning me.
 

laserpp

Distinguished
Nov 29, 2008
137
0
18,630
Okay, tried to change it up a little but still no luck.

Code:
#include <iostream>
using namespace std;


double celsius(int);

int main()
{

	double c;

	for (int x = 0; x <= 20; x++)
	{
		c = celsius(x);
		cout << "The Temperature in Fahrenheit is " << x << "	   The Temperature in Celsius is " << c << endl;
	}

	return 0;
}

double celsius(int x)
{
	double total;
	total = ((5/9)*(x - 32));
	return total;
}

Here is the output:
Untitled.png
 

Bruceification73

Distinguished
Oct 5, 2009
169
0
18,660
This works:

Code:
#include <iostream>
using namespace std;

double celsius(int x);

int main()
{

	double c;

	for (int x = 0; x <= 20; x++)
	{
		c = celsius(x);
		cout << "The Temperature in Fahrenheit is " << x << "	   The Temperature in Celsius is " << c << endl;
	}
	system("pause");
	return 0;
}

double celsius(int x)
{
	double total;
	total = ((5.0/9.0)*(x - 32.0));
	return total;
}
 
Solution

flamethrower205

Distinguished
Jun 26, 2001
187
0
18,630
I think important takeaway from this (and I see this error all of the time; it's very common) is the distinction between double and int arithmetic. The compiler evaluates everything within parenthesis first, so a statement such as (5/9) will be treated as integer arithmetic rather than double. This means that 5/9 = 0, so that the rest of your calculation will be zeroed out. Another common mistake is when a programmer wishes to take steps between two ints a and b and decides that the step size should be
double step = (b - a) / (numSteps - 1)
The intended result is for a double, but all of the arithmetic is done as ints, so that step usually becomes 0. The solution is to ask for
(b - a) / (numSteps - 1.0)
This works because the rule of thumb is that when doing arithmetic between a double and an int, the int gets upgraded to a double. However, as mentioned above, (5/9) is int/int, which is just 0.
Hope this clarifies things!