samedi 9 septembre 2017

My C++ code will never be executed past the else statement

just starting out here. Xcode is telling me this "code will never be executed on the first line within the else statement and I don't know what to do about it.

I am trying to make it so that if the user's input is not one the four options in the switch statement it defaults to the third option. Please help.

int lengthoption;
std::cout << "Please select an option for the length of your loan based on the following options: " << std::endl;
std::cout << "1. 3 years" <<std::endl;
std::cout << "2. 4 years" <<std::endl;
std::cout << "3. 5 years" <<std::endl;
std::cout << "4. 6 years" <<std::endl;
std::cin >> lengthoption;


double NumberofPayments;
if (lengthoption == 1 || 2 || 3 || 4)
{
    switch (lengthoption)
    {
        case 1:
            NumberofPayments = 36;
            std::cout << "You chose a 3 year loan with a total of " << NumberofPayments << " monthly payments." << std::endl;
            break;
        case 2:
            NumberofPayments = 48;
            std::cout << "You chose a 4 year loan with a total of " << NumberofPayments << " monthly payments." << std::endl;
            break;
        case 3:
            NumberofPayments = 60;
            std::cout << "You chose a 5 year loan with a total of " << NumberofPayments << " monthly payments." << std::endl;
            break;
        case 4:
            NumberofPayments = 72;
            std::cout << "You chose a 6 year loan with a total of " << NumberofPayments << " monthly payments." << std::endl;
    }
}

else
{
    ***NumberofPayments = 60;***
    std::cout << "You chose a 5 year loan with a total of " << NumberofPayments << " monthly payments." << std::endl;
}

Aucun commentaire:

Enregistrer un commentaire