C ++ if else表达式错误

时间:2013-11-07 16:35:31

标签: c++ if-statement nested

我检查了我的嵌套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 个答案:

答案 0 :(得分:1)

我的猜测是:如果有&lt; 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;
}

答案 1 :(得分:1)

其他需要从if开始,你没有用}

终止
if () {

} else if() {

}

答案 2 :(得分:0)

你做不到:

if (...) {
    ...
    else {
        ...
    }
}
else中的条件不满足时,

if表示替代路径,将其置于此if内是没有意义的。定义elseelse if的唯一方法是在if范围之后:

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

答案 3 :(得分:0)

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

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