1

我有我的程序在这里。

#include <iostream>
#include <string>
using namespace std;
int main()
{
string input;
cout << "say yes or no" << endl;
cin >> input;
if(input == Yes)
{
    cout << "test" << endl;
}
else if(input == No)
{
    cout << "test123" << endl;
}
system("pause");
}

它说是和否是未定义的?请帮助我是 C++ 新手

4

2 回答 2

1

您应该对字符串“是”“否”使用双引号

于 2013-06-06T15:44:06.493 回答
-2

首先,没有双引号的 Yes 和 No 被编译器读取为变量,并且您没有声明任何 Yes 或 No 变量。

其次,我认为您不能像在 C++ 中那样比较 String :您必须使用比较(http://www.cplusplus.com/reference/string/string/compare/)所以程序将是:

#include <iostream>
#include <string>
using namespace std;
int main()
{
  string input;
  cout << "say yes or no" << endl;
  cin >> input;
  if(input.compare("Yes"))
  {
    cout << "test" << endl;
  }
  else if(input.compare("No"))
  {
    cout << "test123" << endl;
  }
  system("pause");
}
于 2013-06-06T15:48:21.617 回答