C++否则表达式错误
C++ if else expression error
我检查了我的嵌套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
中是没有意义的。定义else
或else 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;
}
相关文章:
- 在函数中使用 const int size 参数创建数组会在 Visual Studio 中抛出错误 C++:表达式的计
- 错误:表达式必须是可修改的 I值
- 错误:表达式必须具有算术、无作用域枚举或带有运算符重载的指针类型
- 错误:表达式不能用作函数
- C++/错误:表达式必须具有整数或无作用域枚举类型
- 错误表达式必须是插入函数(二叉树 C++)中的可修改 Ivalue
- 合并排序错误 - 表达式必须具有常量值
- 数组声明中的错误:表达式必须具有常量值
- 错误:表达式列表在初始化器[-fpermissive] Double Paycalc(p,yir,y)中被视为复合表达式
- 错误:表达式不能用作函数
- 我该如何解决此错误:表达式必须具有恒定值.环境是2013年
- 错误:表达式必须是可修改的LVALUE
- 错误:表达式必须具有指向类C++的指针
- 错误:表达式不能用作函数
- 错误:表达式必须具有积分或未引人注目的枚举类型
- 参数化sc_lv时报告错误(表达式必须具有常量值)
- 无法使用函数变量?错误表达式必须具有类类型
- 错误:表达式必须具有类类型
- 错误:表达式不能用作函数.如何删除它
- C++ - 错误:表达式必须具有指向对象的指针类型