dimanche 28 octobre 2018

If statement being ignored in c++

The source code I have written (Mind you, I am a new-learner so I am currently working my way through a book to teach myself).

Everything seems to work, but the code seems to ignore the statement "if (fahrenheit >= 80). It works with the first statement parameters, but will jump to the else condition for all numbers above 79 and below 50, rather than outputting different text for values above 80.

Advice?

#include <iostream>

float convert(float);

int main()
{
    float fahrenheit;
    float celsius;

    std::cout << "Please enter the temperature in Celsius: ";
    std::cin >> celsius;
    fahrenheit = convert(celsius);
    std::cout << "\nThe temperature in Fahrenheit is: " << fahrenheit << "\n";


if ((fahrenheit <= 79) && (fahrenheit >= 50))
{
    if (fahrenheit >= 80)
    {
        std::cout << "\n What a hot day!\n";
        return 0;
    }

    std::cout << "\n What a nice day!\n";
}
    else
        std::cout << "\n It's freezing!\n";

    return 0;
}   

float convert(float celsius)
{
    float fahrenheit;
    fahrenheit = ((celsius * 9) / 5) +32;
    return fahrenheit;
}

Aucun commentaire:

Enregistrer un commentaire