我写了一个简单的例子来解决我在编写程序时遇到的问题。
在程序执行期间,当从函数返回值时,我获得了input1和input2的值,这些值随后永远不会改变。然后,在程序的各种计算过程中稍后,我得到了一个结果,这个结果也再也不能改变。
我试图使用switch-case进行比较,但是我得到了一个错误:“'input1'的值在常量表达式中无法使用”。
在程序执行期间,当从函数返回值时,我获得了input1和input2的值,这些值随后永远不会改变。然后,在程序的各种计算过程中稍后,我得到了一个结果,这个结果也再也不能改变。
我试图使用switch-case进行比较,但是我得到了一个错误:“'input1'的值在常量表达式中无法使用”。
#include <iostream>
using namespace std;
char getChar()
{
char c;
cin >> c;
return c;
}
int main()
{
// it doesn't work
const char input1 = getChar();
const char input2 = getChar();
// it it works
//const char input1 = 'R';
//const char input2 = 'X';
char result = getChar();
switch(result)
{
case input1:
cout << "input1" << endl;
break;
case input2:
cout << "input2" << endl;
break;
}
return 0;
}