如何让用户在此程序中输入文本而不是数字。我怎样才能让 cin 声明接受文本?我必须使用char吗?
int main()
{
using namespace std;
int x = 5;
int y = 8;
int z;
cout << "X=" << x << endl;
cout << "Y=" << y << endl;
cout << "Do you want to add these numbers?" << endl;
int a;
cin >> a;
if (a == 1) {
z = add(x, y);
}
if (a == 0) {
cout << "Okay" << endl;
return 0;
}
cout << x << "+" << y << "=" << z << endl;
return 0;
}
---编辑---为什么这不起作用?
int main()
{
using namespace std;
int x = 5;
int y = 8;
int z;
cout << "X = " << x << " Y = " << y << endl;
string text;
cout << "Do you want to add these numbers together?" << endl;
cin >> text;
switch (text) {
case yes:
z = add(x, y);
break;
case no: cout << "Okay" << endl;
default:cout << "Please enter yes or no in lower case letters" << endl;
break;
}
return 0;
}
谢谢大家!如果你有兴趣,你可以看看我在这里制作的游戏。 http://pastebin.com/pmCEJU8E 您正在帮助一位年轻的程序员实现他的梦想。