【发布时间】:2016-03-12 05:25:06
【问题描述】:
编译以下代码时,我收到以下错误消息:
错误 C2131 表达式未计算为常量
这出现在所有“案例”行上,例如
case (x == 10):
这是代码:
#include <iostream>
using namespace std;
int main()
{
int x;
cout << "Please enter your value for x" << endl;
cin >> x;
cout << "The value you entered for x is " << x << endl;
switch (x)
{
case (x == 10) :
{
x = x + 10;
cout << "x is " << x << endl;
}
case (x == 20) :
{
x = x + 20;
cout << "x is " << x << endl;
}
case (x == 30) :
{
x = x + 30;
cout << "x is " << x << endl;
}
case:
{
cout << "x is " << 2 * x << endl;
}
}
}
我意识到我一定是错误地使用了 switch 语句,有人可以纠正我吗? 谢谢。
【问题讨论】:
-
您是否尝试查看任何示例代码?有任何关于 C++ 的教程或书籍吗?
标签: c++ visual-studio debugging visual-studio-debugging