0

该项目的方向如下:

“对于活动级别,如果输入无效,则打印一条消息并告诉用户您假设他们是久坐的。”</p>

以下是我当前的代码,我正在尝试将其设置为"Sedentary"用户可以输入为 SA 或 sa 的默认值。无效输入的默认设置应该使程序默认为久坐,我有点困惑我是否在正确的轨道上。

else if (gender == 'F' || gender == 'f') {
    cout << "Please enter your Height in inches. (You may include a decimal) ";
    cin  >> height;

    cout << "Please enter your Weight in pounds as a whole number. ";
    cin  >> weight;

    cout << "Please enter your Age in years. ";
    cin  >> age;
    cout << endl;

    if (activity_level = 'SA' || activity_level = 'sa' || activity_level = 'LA' || activity_level = 'la' || 
        activity_level = 'MA' || activity_level = 'ma' || activity_level = 'VA' || activity_level = 'va') {
        cout << "Please enter your activity level." << endl << endl;
        cout << "You may enter one of the following choices." << endl << endl;
        cout << "Sedentary (Little or no exercise)   \"SA\" or \"sa\" " << endl;
        cout << "Lightly Active (Light exercise/sports 1-3 days a week)   \"LA\" or \"la\" " << endl;
        cout << "Moderately Active (Moderate exercise/sports 3-5 days a week)   \"MA\" or \"ma\" " << endl;
        cout << "Very Active (Hard exercise/sports 6-7 days a week)   \"VA\" or \"va\" " << endl << endl;
        cout << "Enter your activity level now. ";
        cin  >> activity_level;
        cout << endl;
    }
    // Output for the  message to defualt to Sedentary if you do not select activity level throught he proper input.
    else {
        activity_level =
        cout << "I'm sorry I did not recogonize that activity level. We will assume a sedentary amount of exercise. "
    }
}

基本上我想知道,如果我在做什么;在语句中使用另一个 if 语句else if将起作用,我想知道我目前设置它的方式是否会产生所需的结果。

4

2 回答 2

0

您必须在变量“activity_level”被赋值后检查它。您还应该使用 == 进行比较。你可以使用!运算符来否定条件。

于 2013-02-14T16:37:54.283 回答
0

如果你想默认,SA那么你可以这样做:

    //Assume activity_level has had something assigned to it, to compare to.

    if (activity_level == "SA" || activity_level == "sa" || activity_level == "LA" || activity_level == "la" || 
        activity_level == "MA" || activity_level == "ma" || activity_level == "VA" || activity_level == "va")
    {
        //Do stuff if input is valid
    }
    // Output for the  message to defualt to Sedentary if you do not select activity level throught he proper input.
    else
    {
        activity_level = "SA";
        std::cout << "I'm sorry I did not recogonize that activity level. We will assume a sedentary amount of exercise.";
    }

单引号也只能是一个单引号char,其他任何东西都需要双引号,因为它是一个字符串。

于 2013-02-14T16:38:34.903 回答