C++ 字符串匹配返回 False

0 c++ visual-c++

我正在做我的 C++ 作业。我在字符串比较方面遇到问题。

我使用 == 操作比较两个明显相同的字符串,但条件返回 false。调试器还显示两个字符串(存储在不同的变量中)是相同的。我肯定错过了什么。

这是我的代码:

void classCounter() {

    ifstream fread;
    string linetxt;
    char *records[50];
    char myLine[100];
    char delims[] = "|";
    int btotal=0,etotal=0,total=0;

    fread.open("F:\\myfile.txt");

    while(!fread.eof()) {

        getline(fread,linetxt,'\n');

        int i = 0;
        strcpy(myLine, linetxt.c_str());
        records[i] = strtok( myLine, delims );

        while( records[i] != NULL  ) 
        {
            cout << records[i] << "|";

            char *bu = "Business";

            if(records[i] == bu) {
                btotal++;

            }
            if(records[i] == "Economy") {
                etotal++;

            }

            //printf("%d '%s'\n", i, records[i]);
            records[++i] = strtok( NULL, delims );
            break;
        }

        total++;
    }

    cout << "Total number of booked Business seats: " << btotal << endl;
    cout << "Total number of booked Economy seats: " << etotal << endl;

    cout << "Total number of booked seats: " << total << endl << endl;

}
Run Code Online (Sandbox Code Playgroud)

这是调试器显示的内容:

在此输入图像描述

两个 if 条件都返回 false。

请建议可能是什么问题。

Som*_*ude 5

您正在比较两个指针,它们永远不会相同。要么注意使用建议std::string(我也推荐),要么使用它strcmp来比较字符串。