这是C++控制台片段。如何用C++中的参数和用户控制台输入调用函数?
我希望根据用户输入调用一个保持参数的函数。
例如:
#include<iostream>
using namespace std;
void Add (int x, int y)
{
cout << x + y << endl;
}
void Subs (int x, int y)
{
cout << x - y << endl;
}
int main(int argc, char* argv[])
{
// Variable initialization
char calc_type;
int x;
int y;
// Console input
cout << "Add or Substract (a or s)?" << endl;
cin >> calc_type;
cout << "1st number" << endl;
cin >> x;
cout << "2nd number" << endl;
inc >> y;
if (calc_type == "a")
{
Add(x, y);
}
else
{
Subs(x, y);
}
return 0;
}
但在写这篇我返回的错误信息,如如下:
错误C2446: '==':没有从 '为const char *' 到'转换INT”
没有上下文,其中该转换是可能的
错误C2040: '==': 'INT' 在从间接水平不同 '为const char [2]'
如何可以解决这个问题应付(也许引用或指针是优选???)
谢谢
要挑剔,字符串文字不是指针。 – chris 2012-07-16 14:53:34