dimanche 17 mars 2019

c++ program runs incorrect else statement

When I run the program, I can't figure out why when I ask for the letter a, b, or c and a different letter is inputted other than those, why is it going to "invalid criteria" instead of "An invalid code has been entered. When I input a number out of the range it goes to "Invalid number has been entered" but not when I'm asking for the letter

#include <iostream>
#include <iomanip>
#include "Header.h"

using namespace std;

int main()
{
    head();

    int num_1, a, b, c;
    char char_1;
    float num_2;
    num_2 = 28.82;

    a = b = c = 0;

    cout << "Please enter a number between 10 and 30." << endl;
    cin >> num_1;

    if (num_1 >= 10 && num_1 <= 30)
    {
        cout << "Enter the letter a, b, or c." << endl;
        cin >> char_1;

        if (char_1 == 'a'||'b'||'c')
        {

            if ((num_1 >= 10 && num_1 <= 20) && (char_1 == 'a'))
            {

                num_2 = num_2 + .5;
                cout << fixed << setprecision(1) << num_2 << endl;

            }
            else if ((num_1 >= 19 && num_1 <= 30) && (char_1 == 'b'))
            {
                num_2 = num_2 + .10;
                cout << fixed << setprecision(2) << num_2 << endl;

            }
            else if ((num_1 >= 19 && num_1 <= 30) && (char_1 == 'c'))
            {
                num_2 = num_2 + .100;
                cout << fixed << setprecision(3) << num_2 << endl;
            }
            else
            {
                cout << "Invalid criteria" << endl;

            }


        }
        else 
            cout << "An invalid code has been entered." 


    }

    else
        cout << "An invalid number has been entered." << endl;




    system("pause");
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire