Sign in with
Sign up | Sign in
Your question
Solved

C++ program

Tags:
  • Programming
  • Celsius
  • Temperature
  • Apps
Last response: in Apps General Discussion
Share
May 9, 2010 1:10:06 AM

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:


  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. int celsius(int);
  6.  
  7. int main()
  8. {
  9.  
  10. int c;
  11.  
  12. for (int x = 0; x <= 20; x++)
  13. {
  14. c = celsius(x);
  15. cout << "The Temperature in Fahrenheit is " << x << " The Temperature in Celsius is " << c << endl;
  16. }
  17.  
  18. return 0;
  19. }
  20.  
  21. int celsius(int x)
  22. {
  23. return ((5/9)(x - 32));
  24. }


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.

More about : program

May 9, 2010 4:14:17 AM

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:
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. double x , c;
  7. for(x=0;x<21;x++)
  8. {
  9. c = (5.0 / 9.0) * (x - 32.0);
  10. cout<<"The temperature in fahrenheit is: "<<x<<"\tThe temperature in celsius is: "<<c<<endl;
  11. }
  12. system("pause");
  13. return 0;
  14. }


This is working for me.
m
0
l
May 9, 2010 4:14:21 AM

But you may want to use higher values for X, because with 0-20 you get all negative numbers for C.
m
0
l
Related resources
May 9, 2010 5:35:22 AM

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.
m
0
l
May 9, 2010 5:52:58 AM

Okay, tried to change it up a little but still no luck.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4.  
  5. double celsius(int);
  6.  
  7. int main()
  8. {
  9.  
  10. double c;
  11.  
  12. for (int x = 0; x <= 20; x++)
  13. {
  14. c = celsius(x);
  15. cout << "The Temperature in Fahrenheit is " << x << " The Temperature in Celsius is " << c << endl;
  16. }
  17.  
  18. return 0;
  19. }
  20.  
  21. double celsius(int x)
  22. {
  23. double total;
  24. total = ((5/9)*(x - 32));
  25. return total;
  26. }


Here is the output:
m
0
l

Best solution

May 9, 2010 6:23:20 AM

This works:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. double celsius(int x);
  5.  
  6. int main()
  7. {
  8.  
  9. double c;
  10.  
  11. for (int x = 0; x <= 20; x++)
  12. {
  13. c = celsius(x);
  14. cout << "The Temperature in Fahrenheit is " << x << " The Temperature in Celsius is " << c << endl;
  15. }
  16. system("pause");
  17. return 0;
  18. }
  19.  
  20. double celsius(int x)
  21. {
  22. double total;
  23. total = ((5.0/9.0)*(x - 32.0));
  24. return total;
  25. }

Share
May 9, 2010 6:24:13 AM

Just changed int to int x in the function call and added .0 to the values.
m
0
l
May 9, 2010 7:13:55 AM

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!
m
0
l
May 9, 2010 7:17:02 AM

yeah, I always use double if decimals will be involved or if there's math like he had.
m
0
l
May 9, 2010 7:31:17 AM

The beauty is what happens when you have untyped languages, like python. Then all sorts of heinous arithmetic errors happen cause of variable confusion.
m
0
l
May 9, 2010 10:41:31 AM

Yeah, I don't have too much experience with python, but I can imagine how it would be a lot different.
m
0
l
May 9, 2010 4:30:06 PM

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
m
0
l
May 9, 2010 11:35:50 PM

Best answer selected by laserpp.
m
0
l
!