jeudi 28 octobre 2021

using for loop and if statements

The output will not stop after I input 6 ages, it wants a 7th age, but I feel like it should be i <= 6. When I change it to i <= 5 it doesn't change it to 6 ages it changes to 5 aes so I'm not sure what's wrong.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    //declare variables
    int age;

    cout << "Enter the age of 6 friends: ";
    cin >> age;

    for (int i = 1; i <= 6; ++i)
    {
        if (age >= 0 && age < 13)
        {
            cout << age << "\tChild" << endl;
        }
        if (age >= 13 && age <= 19)
        {
            cout << age << "\tTeenager" << endl;
        }
        if (age > 19)
        {
            cout << age << "\tAdult" << endl;
        }
        cin >> age; 
    }

    //terminate program
    system("pause");
    return 0;
}

Aucun commentaire:

Enregistrer un commentaire