Can someone please tell me what I did wrong? my program won't solve for the answers. It's my first program using functions.
Here's the problem with the program:
for celsius to fahrenheit, the output of celsius is displayed but the output for fahrenheit is always 0.00 (ex. 25.5 C is 0.00 F)
for fahrenheit to celsius, the output of fahrenheit is displayed but the output for celsius is the value that was used for the previous conversion (ex. 53.6 F is 25.5 C)
for celsius to kelvin, the output of celsius is displayed but the output for kelvin is always 0.00 (ex. 1 C is 0.00 K)
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
double CtoF (double a, double b)
{
cout<<fixed<<setprecision(2);
double c, f;
f = 9/5 * c + 32;
return f;
}
integer math. 9/5 is 1
5/9 is zero
just add a decimal:
9/5.0
5/9.0
etc
also please use code tags. <> on the editor or code & /code in [] brackets
its <cmath> not math.h (math.h is for C programs)
c is not initialized in at least some of your functions.
useless variables are useless. harmless, but clutter etc.
1 2 3 4 5 6 7 8 9
consider
double CtoF (double degf); //a, double b) //what are a and b, and why not use them somewhere? and no ; on this line!!!
{
cout<<fixed<<setprecision(2); //this does not belong here. it belongs near where you print.
double c, f; //what is C??
return 1.8 * degf +32; //?? what is C?
f = 9/5 * c + 32;return f;
}
for celsius to fahrenheit, the output of celsius is displayed but the output for fahrenheit is always 0.00 (ex. 25.5 C is 0.00 F)
for fahrenheit to celsius, the output of fahrenheit is displayed but the output for celsius is the value that was used for the previous conversion (ex. 53.6 F is 25.5 C)
for celsius to kelvin, the output of celsius is displayed but the output for kelvin is always 0.00 (ex. 1 C is 0.00 K)
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Secondly, but more repeatedly in your coding, you are ignoring integer division. The first mathematical operation done in that line is
5 / 9
Since 5 and 9 are both int constants, the result is an int constant. The result will be truncated to the next integer below for positive results, so it will yield 0. If you wrote the ostensibly more correct 9/5 it would still truncate down to 1.
Either just add a decimal point to one of the numbers (e.g. 5.0 instead of 5) or, once you have written the fraction the correct way up, use the end result 1.8.
I am well aware that you get away with this in Python 3, but C++, in common with most other languages, does integer division when the operands are integers.
5/9 and 9/5 use integer arithmetic - so the result is an integer (0 and 1 respectively). To get the division to return a real result, one (or both) of the dividend and divisor needs to be of type real. Hence using 9.0 instead of 9. 9.0 is a real number so the result is real.