使用Char作为条件变量

时间:2016-03-28 06:41:21

标签: c++ if-statement char

我需要帮助添加到一个程序,该程序对年龄进行分类,以确定个人是否有资格获得人寿保险。规则是添加到以前的程序,允许用户输入年龄,然后输出他们是未成年子女,未成年少年,成年人或高级成年人。需要使用char yes或no来决定是否符合条件。此增强规则也将确定资格。以下是目前的规则和代码。

年龄符合条件 17或'n'下 18-90'y' 超过90'n'< ------新的增强规则  代码:

//12 or under         minor child
//13 - 17             minor teenager
//18 - 64             adult
//65 or over      senior adult


#include <iostream>
using namespace std;
int main()
{
    const int MAX_CHILD = 12, MAX_TEEN = 17, MAX_ADULT = 64; MAX_ADULT_ALLOWED = 90;
    int age;


    cout << "How old are you?";
    cin >> age;
    // additional code goes here

    if (age <= MAX_CHILD)
        cout << "You're a minor child.\n";

    else if (age <= MAX_TEEN)
        cout << "You're a minor teenager.\n";

    else if (age <= MAX_ADULT)
        cout << "You're an adult.\n";

    else if
        cout << "You're a senior adult.\n";

    else (age <= MAX_ADULT_ALLOWED)



    //if (eligible == "y")
    //cout << "You are eligible for life insurance package.\n";
    return 0;
}

2 个答案:

答案 0 :(得分:0)

我希望这会回答你的问题:

#include "stdafx.h"
#include <iostream>
#include <string>



using namespace std;

//12 or under         minor child
//13 - 17             minor teenager
//18 - 64             adult
//65 or over      senior adult

int main()
{
    const int MAX_CHILD = 12, MAX_TEEN = 17, MAX_ADULT = 64, MAX_ADULT_ALLOWED = 90;
    int age;
    char eligible ;

    cout << "How old are you?";
    cin >> age;
    // additional code goes here

    if (age <= MAX_CHILD)
        {
            cout << "You're a minor child.\n";
            eligible = 'n';
        }

    else if (age <= MAX_TEEN)
        {
            cout << "You're a minor teenager.\n";
            eligible = 'n';
        }

    else if (age <= MAX_ADULT)
        {
            cout << "You're an adult.\n";
            eligible = 'y';
        }

    else if (age <= MAX_ADULT_ALLOWED)

        {
            cout << "You're a senior adult.\n";
            eligible = 'y';
        }

    else

        {
            cout << "You're a senior adult.\n";
            eligible = 'n';
        }




    if (eligible == 'y')

        cout << "You are eligible for life insurance package.\n";

    else

         cout << "You are not eligible for life insurance package.\n";

    std::system("PAUSE");
    return 0;
}

答案 1 :(得分:0)

您可以将打印语句 cout << "You are eligible for life insurance package.\n";放在满足条件的if中,不需要额外的变量来获取资格。

if (age <= MAX_CHILD){
    cout << "You're a minor child.\n";
    cout << "You are not eligible for life insurance package.\n";
    //eligible = 'n';

}
else if (age <= MAX_TEEN){
    cout << "You're a minor teenager.\n";
    cout << "You are not eligible for life insurance package.\n";
    //eligible = 'n';
}

else if (age <= MAX_ADULT){
    cout << "You're an adult.\n";
    cout << "You are eligible for life insurance package.\n"; 
    //eligible = 'y';
}

else if(age <= MAX_ADULT_ALLOWED){
    cout << "You're a senior adult.\n";
    cout << "You are eligible for life insurance package.\n";
    //eligible = 'y';
}

else {
    cout << "You are not eligible for life insurance package.\n";
    //eligible = 'n';
}

或者,如果你想使用一个单独的变量来获得资格,那么只需在块内分配值 y 即可满足条件,否则分配 n (我已评论过)代码中的那些行),最后检查资格。