 # Incorrect Arithmetic Result

I am currently working on the variable section of in the C++ course. The exercise on which it asks me to ask user what the temperature is using cin. I used the formula that they gave me to find degrees in Celsius, C = (F-32)/1.8.
I did the following code:

#include int main() { double tempf; double tempc; tempc = (tempf - 32) / 1.8; std::cout << "Enter the temperature in Fahrenheit: "; std::cin >> tempf; std::cout << “The temperature is " << tempc << " degrees Celsius.\n”; }

#include

int main() {

double tempf;
double tempc;

tempc = (tempf - 32)
/ 1.8;

std::cout << "Enter the temperature in Fahrenheit: ";
std::cin >> tempf;
std::cout << “The temperature is " << tempc << " degrees Celsius.\n”;
}

For some reason, when I put 83, which was the value that they had assigned me on the previous exercise. (I used it as an example to see if I would get the same results), I get -17.77778 Degrees Celsius. It turns out that this answer would appear for any value that I put for degrees Fahrenheit. Please help. Thanks.

Be careful with the order of execution. As for 17.77778, what’s 32 / 1.8, that might give you a hint for the value of `tempf` at the time `tempc` is calculated.

1 Like

The previous exercise before the one above gave me a formula on how to calculate Celsius. The formula is
(C)elsius = ( (F)arenheit-32 ) /1.8. I’m not too sure what you mean by “order of execution”. I am very new to programming so I don’t really understand much. Sorry.

UPDATE:
I found that the value for tempf (Farenheit) is not being substituted and (-32) is dividing by 1.8 by themselves. I am still not too sure why this is happening since I included “tempf” in the formula of finding the degrees in Celsius.

No worries, it takes time. Consider the following quick example, what do you expect `y` to be here?

``````x = 3
y = x + 5
x = 0
y = ?
``````

Do you think the final change to `x` affects the value of `y`?

For the majority of languages I can think of the answer is no it doesn’t, the section where `y` is calculated is run once. The value is calculated at that point, changing `x` again will not affect `y` unless you explicitly asked it to be recalculated. This is the way your code works, it’s a series of steps, they run one by one (imperative).

So does that mean that my code cannot substitute the x (Fahrenheit) value of 83 unless I explicitly asked it to be recalculated? Is there anyway I can do this?

I just realized that the system itself brings out the formula, for the calculation of Celsius, after I ask the user first what the degrees in Fahrenheit is. I was really thinking about the “order” that you were mentioning about and I finally got it. I had written the formula before asking the user what the temperature in Fahrenheit was. Thanks for the reply!

1 Like

You’ll come across something suitable soon enough when functions are introduced, whicht acts more like a formula (though you will have to call the function to obtain the result of the calculation).

For a value that changes in the background you’d probably want to be working with objects which probably won’t come up for a while in your course. Sometimes having everything clearly laid out makes it more readable, values that change implicitly can be harder for someone to follow so don’t rush into including such options.

1 Like