-2

我检查了嵌套的 if 语句和右括号......不知道为什么,但在编译时出现“预期表达式”错误。错误发生在这段代码中:else if (secondCourseEnrollCount >= 20)

这是我的代码:

#include <iostream>
#include <string>
#include <stdlib.h>
using namespace std;

int main()
{
    string myName, firstCourseID, secondCourseID, thirdCourseID;
    int studentID, firstCourseEnrollCount, secondCourseEnrollCount, thirdCourseEnrollCount;


    firstCourseEnrollCount=secondCourseEnrollCount=20;

    thirdCourseEnrollCount=17;

    cout << "Please Type Your Name: ";

    cin >> myName;

    cout << "Please Type Your Student ID Number without spaces: ";

    cin >> studentID;

    cout << "Please Type Your First Requested Course ID: ";

    cin >> firstCourseID;

    cout << "Please Type Your Second Requested Course ID: ";

    cin >> secondCourseID;

    cout << "Please Type Your Third Requested Course ID: ";

    cin >> thirdCourseID;

    // First If Statement Should Come Here
    if (firstCourseEnrollCount >= 20)
    {
        // True Path Here
        cout << "The first choice currently has 20 Students, " << myName << endl;

        cout << firstCourseID << " is Already at Maximum Enrollment, so you have not been added to the class";



        // False Path Here
        // Second If Statement Here
            else if (secondCourseEnrollCount >= 20)
            {
                // True Path Here

                cout << "The second choice currently has 20 Students, " << myName << endl;

                cout << secondCourseID << " is Already at Maximum Enrollment, so you have not been added to the class";

                // False Path Here
                // 3rd If Statement Here
                    else if (thirdCourseEnrollCount >= 20)
                    {
                        // True Path Here

                        cout << "The third choice currently has 20 Students, " << myName << endl;

                        cout << thirdCourseID << " is Already at Maximum Enrollment, so you have not been added to the class";

                        //False Path Here
                        else
                        {
                            cout << "All Three Courses Have Seats Remaining, " << myName << endl;

                            cout << "You have been enrolled in " << firstCourseID << endl;

                            cout << "You have been enrolled in " << secondCourseID << endl;

                            cout << "You have been enrolled in " << thirdCourseID << endl;
                        }
                    }// End 3rd If Statement

            }// End 2nd If Statement
    }// End 1st If Statement



    return 0;
}
4

4 回答 4

1

我的猜测是:如果有 <20 名学生,就注册 course1,否则尝试 course2,否则尝试 course3,否则无事可做,抱歉。

if (firstCourseEnrollCount >= 20)
{
    cout << "The first choice currently has 20 Students, " << myName << endl;
    cout << firstCourseID << " is Already at Maximum Enrollment, so you have not been added to the class";

    if (secondCourseEnrollCount >= 20)
    {
        cout << "The second choice currently has 20 Students, " << myName << endl;
        cout << secondCourseID << " is Already at Maximum Enrollment, so you have not been added to the class";

        if (thirdCourseEnrollCount >= 20)
        {
            cout << "The third choice currently has 20 Students, " << myName << endl;
            cout << thirdCourseID << " is Already at Maximum Enrollment, so you have not been added to the class";
            cout << "Nothing to do, sorry" << endl;
        }
        else
            cout << "You have been enrolled in " << thirdCourseID << endl;
    }
    else
        cout << "You have been enrolled in " << secondCourseID << endl;
}
else
{
    cout << "All Three Courses Have Seats Remaining, " << myName << endl;
    cout << "You have been enrolled in " << firstCourseID << endl;
}
于 2013-11-07T16:37:45.413 回答
1

else 需要从 if 开始,你没有用 } 终止

if () {

} else if() {

}
于 2013-11-07T16:37:55.050 回答
0

你不能这样做:

if (...) {
    ...
    else {
        ...
    }
}

else表示不满足条件时的替代路径,if将其放在 this 中没有任何意义ifelse定义or的唯一方法else if是在 的范围之后if

if (...) {
    ...
}
else {
    ...
}
于 2013-11-07T16:38:52.850 回答
0

C/C++ 中 if ... else 的语法是

if (condition)
  statement1;
else
  statement2;

如果您想要多个语句,请使用括号

if (condition)
{
  statement1;
  statement2;
}
else
  statement3;

an 之后的语句else可以是 another if,这就是您要执行的操作:

if (condition1)
{
  statement1;
  statement2;
}
else
  if (condition2)
  {
    statement3;
    statement4;
  }

但是额外的缩进级别看起来令人困惑,因此通常格式化为

if (condition1)
{
  statement1;
  statement2;
}
else if (condition2)
{
  statement3;
  statement4;
}
于 2013-11-07T16:42:24.220 回答