Fairly basic Fahrenheit to Celsius converter not working

Here's a bit of code I can't seem to get to work for the life of me.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

int main()
{
    double converter;
    char cf;
    while(true)
    {
        while (std::cin)
        {
            std::cout << "Enter the value you want to convert." << std::endl;
            std::cin >> converter >> cf;
            if (cf == 'F' || cf == 'f')
            {
                std::cout << "New value: " << ((cf-32)*5)/9 << std::endl << std::endl;
            }
            else if (cf == 'C' || cf == 'c')
            {
                std::cout << "New value: " << ((cf*9)/5)+32 << std::endl << std::endl;
            }
            else
            {
                std::cout << "Error." << std::endl << std::endl;
            }
        }
        std::cin.clear();
    }
}


Entering 0 C should get it to convert to Fahrenheit (and everything should evaluate to double, not int,) so I'm obviously doing something wrong. But what?
Last edited on
1
2
3
4
if (cf == 'F' || cf == 'f')
            {
                std::cout << "New value: " << ((cf-32)*5)/9 << std::endl << std::endl;
            }


You are performing arithmetic on the char rather than the double, it seems.
This is a minor error, heh. You formula's are using "cf", the character, instead of "converter", the value of the temperature. Try adjusting that and see if it works.
Oh. Um. Oops. Thanks!
I think that here

std::cout << "New value: " << ((cf-32)*5)/9 << std::endl << std::endl;

and here

std::cout << "New value: " << ((cf*9)/5)+32 << std::endl << std::endl;

you should use converter instead of cf :)

Topic archived. No new replies allowed.