string.compare for c ++

TT1*_*T12 0 c++ io compare

我要求用户输入一个单词然后让我的程序将它与输入的.txt文件进行比较,但即使我在data.txt中输入一个单词,它仍然执行false.

//------------in my data.txt---------
// Banana Bed Today
// Apples Chair Window
// Corn Tomorrow Hive

string testData;
  cout<<"enter Data: ";
  cin>>testData;
  for(i=0; i<s.size()-1; i++){
    if (testData.compare(s[i]->name) == 0)
      cout<<"Right\n";
    if (youkno.compare(s[i]->name) != 0)
        cout<<"Wrong\n";
  }
Run Code Online (Sandbox Code Playgroud)

如果我提示Banana然后输出出错的错误

pax*_*blo 5

如果你替换:

for(i=0; i<s.size()-1; i++){
Run Code Online (Sandbox Code Playgroud)

有:

for(i=0; i<s.size(); i++){  // Adjusting to check last entry as well.
    cout << "[" << s[i]->name << "][" << testData << "]" << endl;
Run Code Online (Sandbox Code Playgroud)

你可能会发现它变得非常明显.很有可能你的一个字符串不是你所期望的.