繁体   English   中英

比较两个字符串数组C ++

[英]comparing two string arrays c++

我正在验证较大程序的数据文件。 我在代码的最后一部分,试图确保两个单独的数组通过字符串本身彼此“相等”。 我已经包含了最新尝试的代码。 请原谅我,因为我已经发布了整个功能。

这里是:

//
//  validateDataFile.cpp
//  P1
//
//  Created by xxxxxxx on 3/26/13.
//  Copyright (c) 2013 xxxxxxx. All rights reserved.
//

#include "p1.h"

void validateDataFile (string fileName) {
fstream file;
file.open (fileName.c_str());

if (file.is_open()) {
    unsigned int i, j, x = 0, y = 0, a;
    int flag = 0, check = 0;
    string fileData, word, strg[200];

    getline (file, fileData);

    for (i = 0; i < fileData.length(); i++) {
        if ((fileData[i] == ' ') || (fileData[i] < 48) || (fileData[i] > 57)) {
            cout << "fileData[i]: " << fileData[i] << endl;
            cout << "Incorrect DataFile!\nFirst line should contain a positive"
            " integer and no white space" << endl;
            return;
        }
    }

    int numberOfNodes = convertToInt(fileData);

    string list[numberOfNodes];

    if (numberOfNodes < 0) {
        cout << "Number of Nodes: " << numberOfNodes << endl;
        cout << "Incorrect DataFile!\nFirst character should be a positive"
        "integer" << endl;
        return;
    }

    getline (file, fileData);
    stringstream stream (fileData);

    while (getline (stream, word, ' ')) {
        list[x++] = word;

            for (a = 0; a < numberOfNodes; a++) {
                    cout << "list of nodes: " << list[a] << endl;   //testing only
            }
    }

    if (x != numberOfNodes) {
        cout << "Incorrect DataFile!\nList of strings has more strings than"
        " the number of nodes specified in the first line." << endl;
        return;
    }

    while (!file.eof()){
        getline (file, fileData);
        stringstream ss (fileData);

        while (getline (ss, word, ' ')) {


            if (convertToInt(word) < 0) {
                flag = 0;
                for (i = 0; i < y; i++) {
                    if (strg[i] == word) flag = 1;
                }

                if (flag == 0) strg[y++] = word;
            }
        }
    }

    for (i = 0; i < y; i++) {               //<- my problem starts here
        check = 0;
        for (j = 0; j < x; j++) {
            if (strg[i].compare (list[j]) == 0) {
                check = 1;                  //<- my problem ends here
                break;
            }
        }
    }
    if (check == 0) {
        cout << "Incorrect DataFile!\nStrings listed should match Node Strings" << endl;
        return;
    }
}
else {
    cout << "ERROR!\n DataFile not present." << endl;
    return;
}


file.close();

}

它编译没有错误,但没有执行我想要的操作。

我故意更改了数据文件以创建错误,但是由于某种原因,它并不能说明我的比较是错误的。

这是我的数据文件的一小部分:

16
Cape Birmingham Boston Chicago Dallas Detroit KansasCity LosAngeles Memphis Minneapolis Omaha Orlando Richmond SanFrancisco Seattle StLouis 
Atlanta Chicago 718
Atlanta Dallas 781
Atlanta Orlando 439
Birmingham Atlanta 146
Birmingham Detroit 723
Birmingham Richmond 678
Boston Atlanta 1099
Boston Detroit 716
Boston Memphis 1311
Chicago Atlanta 718
Chicago Boston 983
Chicago KansasCity 526

我特意将第一个城市从“亚特兰大”更改为“开普”。 有人可以告诉我我的错误并告诉我需要做些什么来纠正它吗? 谢谢!

如果它是您要对照列表检查的最后一个节点,则只会发现一个错误的值。 您需要将检查循环更改为如下所示:

for (i = 0; i < y; i++) {               //<- my problem starts here
    check = 0;
    for (j = 0; j < x; j++) {
        if (strg[i].compare (list[j]) == 0) {
            check = 1;                  //<- my problem ends here
            break;
        }
    if( check == 0 ) // value not found, break out of verification loop to report this.
        break;
    }

一旦被检查的项目不在列表中,这将停止验证。

代码太多了,很多人(像我一样)甚至都不会开始阅读它。 尝试编写一个更短的程序来重现该问题。

另外,我建议您仔细阅读http://www.cplusplus.com/reference/string/string/compare

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM