Always output negative numbers or zero

Hello,

I'm taking a beginner C++ class here in school and for our assignment this week, we have to make a program to convert Fahrenheit temperatures to Celsius temperatures. The formula isn't a problem, but for some reason, when I execute the program I can only get it to output the number "-18" no matter what. And then, at times, if I mess with the code, all I can get it to output is "0". My teacher mentioned something about "integer division", but that means nothing to me. I've tried everything. Here is my code. Can someone please help me?

/***********************************************************************
* Program:
* Assignment 07, Fahrenheit to Celsius
* Brother Helfrich, CS124
* Author:
* Bryce Matheson
* Summary:
* A simple program to convert a temperature in Fahrenheit
* to Celsius degrees.
*
* Estimated: 0.5 hrs
* Actual: 0.0 hrs
* The most difficult part was getting the
* program to calculate correctly
************************************************************************/

#include <iostream>
#include <iomanip>
using namespace std;

/********************************************************************
* Converts Fahrenheit Temperatures to Celsius
*********************************************************************/

double convertTemp()
{
double C, F;
C = F-32.0 * (5.0/9.0);
return C;
}


/**********************************************************************
* Main: Asks user for imput and returns an answer.
***********************************************************************/
int main()
{
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(0);

double F;
cout << "Please enter Fahrenheit degrees: ";
cin >> F;
double C = convertTemp();
cout << "Celsius: "
<< C
<< endl;
return 0;
}
First of all, C = F-32.0 * (5.0/9.0) may not do what you want. Most likely it will multiply -32 by five-ninths instead of subtracting 32 from F and multiplying that by five-ninths:
C = (F - 32.0) * (5.0/9.0);

If you want to guarantee that the constants are floats, you can append f to the enf of the number like 5.0f.

Also, why are you setting the cout precision to 0? http://www.cplusplus.com/reference/iostream/manipulators/setprecision/

EDIT: Also see below, I missed that >_<
vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
Last edited on
EDIT: Along with LBs changes...

Look at this line
double C = convertTemp();

C is equal to the return value of convertTemp(). Keep that in mind.

Now evaluate your function
1
2
3
4
5
6
double convertTemp()
{
double C, F;
C = (F-32.0) * (5.0/9.0);
return C;
}


C is equal to F-32.0 * (5.0/9.0)
But, what exactly is F ?... you defined it, but it was never set. In this function F is garbage.. some random value made by the compiler.

In main you need to pass your inputted value to convertTemp to be converted.
1
2
cin >> F;
double C = convertTemp(F);


And your function needs to be changed to accept this value
1
2
3
4
5
6
double convertTemp(double F)
{
double C;
C = (F-32.0) * (5.0/9.0);
return C;
}

Last edited on
The double F; in main and double F; in convertTemp() are not the same variable. When your function is called there is a main F and a convertTemp F, both in memory.
Last edited on
Wow! Thanks guys! Thank you VERY much. My program now works. So let me clarify this: my variable "F" in the function main() and my variable "F" in the function convertTemp are two different variables? How can that be? They're the same variable? I thought the point was to accept input from the user in main() and then put that value in the variable "F". Then, function convertTemp() recalls that variable, pulls the value from "F" and then uses it to calculate "C". Is that not correct?

Cazicss, what did the code double C = convertTemp(F); do? Why did you put the variable "F" in the parenthesis? And same with this code: double convertTemp(double F). What does putting the variable inside the parenthesis do?

Thanks for all your help, guys!
The answer is in where a variable exists and where it doesn't.
When you created double F; in main, an 8-byte space was created in memory to store numbers. This space is main's F ... you can create NO other variables named F in main.

Then you did cin >> F;, inputted a number from the user into main's F variable.

By specifying your function with double convertTemp(double F) you are telling this function to create an 8 byte memory space called F, and that the coder must give this function a number to fill that variable up.

When you called the function and passed main's F into the parenthesis double C = convertTemp(F);, at that time "convertTemp's F" is created, and the number you passed "main's F" is copied into that space. so if 32 was in main's F .... then convertTemp's F is created and has 32 copied into it when the function is called.

Then double C; is created in your function. So there is a C in the function being filled up, and a C in main waiting to be filled when the function returns. So you have two 8 byte F variables, and two 8 byte C variables in memory at this point.

Then you return C;. So main's C has the value in convertTemp's C copied into it. After the return all the variables created by the function (convertTemp's F and convertTemp's C) are removed from memory, but thats ok because we already got our answer copied into main's variable C.

-------------
If you change the names of your variables in the function it might make more sense that they are not the same... you could change the function this way and it will still work perfectly, since the names don't matter:
1
2
3
4
5
6
double convertTemp(double Fahrenheit)
{
double Celsius;
Celsius = (Fahrenheit-32.0) * (5.0/9.0);
return C;
}


All the compiler cares about is that it knows what data type it is (double, 8 bytes) and what to call it (F, Fahrenheit, Jimmy,...whatever).
Last edited on
Late but:

First of all, C = F-32.0 * (5.0/9.0) may not do what you want. Most likely it will multiply -32 by five-ninths instead of subtracting 32 from F and multiplying that by five-ninths:
C = (F - 32.0) * (5.0/9.0);


No, it will do what he wants. Just like in algebra, F-X is F minus X, not F times negative X. And C++ requires you to use the * to multiply, you cannot just attach numbers like you can in math.
firedraco: What?
firedraco:

Just like in algebra, F-X is F minus X, not F times negative X.


While this is true, you must realize that c++ also follows the mathematical order of operations. Meaning that if we assume F is equal to 10, then the the following will happen:

C = F-32.0 * (5.0/9.0)

Does this:
a) divide 5.0 by 9.0 = 0.56
b) multiply 32.0 by 0.56 = 17.92
c) subtract 17.92 from F = -7.92 <----[Answer]

The parenthesis are needed for the correct formula:

C = (F-32.0) * (5.0/9.0)

Does this:
a) subtract 32.0 from F = -22.0
b) divide 5.0 by 9.0 = 0.56
c) multiply -22.0 by 0.56 = -12.32 <-----[Answer]

If you google the conversion of 10 degrees Fahrenheit to Celsius, you will find that it will give you approximately -12.23

Since he did not include the parenthesis in his original program, he was correctly told that it
may not do what you want.
Topic archived. No new replies allowed.