C++否则表达式错误

C++ if else expression error

本文关键字:错误 表达式 C++      更新时间:2023-10-16

我检查了我的嵌套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;
}

我的猜测是:如果有 <20 名学生,请注册课程 1,否则尝试课程 2,否则尝试课程 3,否则无事可做,对不起。

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;
}

else 需要遵循 if 之后,您尚未以 } 结尾

if () {
} else if() {
}

你不能做:

if (...) {
    ...
    else {
        ...
    }
}
else表示不

满足if中的条件时的替代路径,将其放在此if中是没有意义的。定义elseelse if的唯一方法是在if范围之后:

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

if 的语法 ...否则在 C/C++ 中是

if (condition)
  statement1;
else
  statement2;

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

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

else后面的语句可以是另一个if,这就是您要执行的操作:

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

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

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