mercredi 8 avril 2020

if in while-loop does not complete

The last name does not get printed and sometimes the names are not alphabetical. Is there an simple and short explanation to this?

int main()
{

    string name1, name2, name3;
    cout << "Enter 3 names: ";
    cin >> name1 >> name2 >> name3;
    int num1 = name1[0], num2 = name3[0], num3 = name3[0];

    int i = 0;
    while(i <= num1 || i <= num2 || i <= num3){
        if (i == num1){
            cout << name1 << ", ";
            ++i;
        }
        else if(i == num2){
            cout << name2 << ", ";
            ++i;
        }
        else if(i == num3){
            cout << name3 << ", ";
            ++i;
        }
        ++i;
    }

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire