Solved

C++ program

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:



#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.
14 answers Last reply Best Answer
More about program
  1. 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:
    
    #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.
  2. But you may want to use higher values for X, because with 0-20 you get all negative numbers for C.
  3. 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.
  4. Okay, tried to change it up a little but still no luck.

    
    
    
    #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:
  5. Best answer
    This works:

    
    
    
    #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;
    }
    
  6. Just changed int to int x in the function call and added .0 to the values.
  7. 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!
  8. yeah, I always use double if decimals will be involved or if there's math like he had.
  9. The beauty is what happens when you have untyped languages, like python. Then all sorts of heinous arithmetic errors happen cause of variable confusion.
  10. Yeah, I don't have too much experience with python, but I can imagine how it would be a lot different.
  11. worked like a charm thank you all very much. Was working for like 6 hours on multiple programs and just forgot about needed .0 for doubles =D
  12. Select best answer then?
  13. Best answer selected by laserpp.
  14. Danke. Glad to be of help.
Ask a new question

Read More

Programming Celsius Temperature Apps