samedi 26 octobre 2019

My fahrenheit-celcius program ignores my if-else statement and changes value to 0 everytime I run the program

My if-else statement doesn't seem to work, anyone have any ideas?

So I created this simple fahrenheit-celcius conversion console app using c++, when I did this, I use a simple if-else statement to determine which conversion they want, fahrenheit-celcius or celcius-fahrenheit. It doesn't work

include

using namespace std;

int main()

{

unsigned short int fahrenheit{}, celsius{};
char unit;

cout << "Choose what unit you want to start with" << endl;

cin >> unit;

if (unit == 'c'){

    cout << "Enter the temperature in Celsius : " << endl;

    cin >> celsius;

    fahrenheit = (celsius * 9.0) / 5.0 + 32;

    cout << "The temperature in Fahrenheit : " << fahrenheit << endl;

}
else {
    cout << "Enter the temperature in Fahrenheit : " << endl;

    cin >> fahrenheit;

    fahrenheit = (celsius * 9.0) / 5.0 + 32;

    cout << "The temperature in Celcius : " << celsius << endl;
}



std::cin.clear(); // reset any error flags
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // ignore any characters in the input buffer until we find an enter character
std::cin.get(); // get one more char from the user

return 0;

}

I expected the output to be working, however, it instantly converst celcius-fahrenheit and completely ignores the std::cin I put to make user input. Instead, they put in the value of 0 everytime I run the program.

Aucun commentaire:

Enregistrer un commentaire