【问题标题】:C++ if statement comparing strings not workingC++ if 语句比较字符串不起作用
【发布时间】:2014-10-14 17:57:22
【问题描述】:

我正在开发一个函数,它读取文件的行,直到文件中到达“XXX”行,并且计数器会跟踪已读取的行数。然后程序计算文件中剩余的行数。我正在使用 if 语句来确定何时中断 while 循环(当读取的行等于“XXX”时)并且不满足条件。即使当 line == "XXX" 时,else 语句仍然会运行。我的代码有什么问题?谢谢!

#include <string>
#include <iostream>
#include <fstream>

using std::string;
using std::endl;
using std::cout;
using std::ifstream;

int main()
{ 
    //Main function
}

void read_file(string input_file_name)
{
    string i_filename = input_file_name;
    ifstream infile;
    infile.open (i_filename);
    string line;
    int num_of_terms1 = 0;
    int num_of_terms2 = 0;

    if (!infile)
    {
        cout << "Your input file could not be opened."<<endl;
    }
    else
    {
        while (!infile.eof())
        {
            getline(infile, line);
            cout << line << endl;

            if (line == "XXX")
            {
                break;
            }
            else
            {
                cout << line << endl;
                num_of_terms1++;
            }
        }
        while (!infile.eof())
        {
            getline(infile, line);
            cout << line << endl;
            num_of_terms2++;
        }
    }
cout << "Terms 1: "<<num_of_terms1 <<endl;
cout << "Terms 2: "<< num_of_terms2 <<endl;
infile.close();
}

这是一个示例输入文件,inputfile.txt:

-2 3
4 2
XXX
-2 3

提前感谢您的帮助!

【问题讨论】:

  • 输出是什么样子的?
  • 你为什么不做一些调试。首先在循环的每次迭代中检查line 的值。您真正的问题不是程序无法运行,而是您还没有学会如何调试。一旦你这样做了,你就可以自己解决所有这些问题。
  • 为什么你认为“即使 line == "XXX",else 语句仍然会运行”?
  • 你检查过空格、车厢等吗?

标签: c++ string if-statement compare


【解决方案1】:

我在 www.compileonline.com 上测试了这段代码并重复了你的发现。

在该环境中,从文件中读取的每个字符串的末尾都有一个 \r 字符。

当我将终止行更改为 if (line == "XXX\r") 时,代码按预期工作。

您的输入文件的行似乎以“\r\n”结尾,这是 Windows 的规范,但 unix 文本文件通常以“\n”结尾。

更新:

这是一个关于如何删除尾随回车和换行符(或任何其他你想要的东西)的小演示:

#include <string>
#include <algorithm>
#include <iostream>

void trim_cruft(std::string& buffer)
{
    static const char cruft[] = "\n\r";

    buffer.erase(buffer.find_last_not_of(cruft) + 1);
}

void test(std::string s)
{
    std::cout << "=== testing ===\n";
    trim_cruft(s);
    std::cout << s << '\n';
}

int main()
{
    test("");                   // empty string
    test("hello world\r");      // should trim the trailing \r
    test("hello\nworld\r\n");   // don't trim the first newline
}

【讨论】:

  • 谢谢!我尝试使用“XXX\n”,但我没有意识到“\r”会这样做。非常感谢您的帮助。
  • 我觉得了解这里发生了什么很重要。在您的测试字符串中嵌入\r 将使其在windows 上工作,但在unix 上不工作。最好在测试之前清除您读取的字符串中的所有控制字符。
【解决方案2】:

首先你应该阅读这个:Why is iostream::eof inside a loop condition considered wrong?

第二个调试行:

cout << line << endl;

在这两种情况下都完全相同-您在 else 语句中,或者您正在计算num_of_terms2,这令人困惑。更改它们,这样您就可以看到打印的是哪一个。

修复该问题后,您会看到“else statement will not still run”

【讨论】:

    【解决方案3】:

    正如我在 cmets 中所说,您有 2 个 cout 语句,您应该验证哪一个正在打印 XXX。如果它们都不是,那么问题很可能是字符串中有回车符,您可以通过以下方式验证其他情况:

    cout << line << endl; // print is here
    
    if (line == "XXX\r")
    {
        break;
    }
    else
    {
        cout << line << endl; // print is here
        num_of_terms1++;
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-06
      • 1970-01-01
      • 2016-07-17
      • 1970-01-01
      • 1970-01-01
      • 2018-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多