1

I am validating a data file for a larger program. I am at the last portion of the code and am trying to make sure two separate arrays are "equal" to each other by the strings themselves. I have included my code of the latest try. Please forgive me as I have posted the whole function.

Here it is:

//
//  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();

}

It compiles with no errors but doesn't do what I want it to.

I purposely changed my data file to create the error but for some reason it does not which tells me my comparison is incorrect.

Here is a small portion of my data file:

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

I purposely changed the first city to "Cape" from "Atlanta." Can someone tell me my error and show me what needs to be done to correct it? Thanks!

5
  • Thats a lot of code. Are you check to see if the files are equal or that if you tokenize every word the arrays of words are equal. If so why can't you use cplusplus.com/reference/algorithm/equal. Commented Mar 28, 2013 at 21:38
  • @rerun Yes it is but I marked in my code where I believe the problem starts and stops. Commented Mar 28, 2013 at 21:41
  • @user1318371 I don't see two arrays, your code is checking strg against strg. Also the easy way to tell if two strings are equal is to use == not compare. Commented Mar 28, 2013 at 21:45
  • @john strg[i] and list[j] looks like arrays to me. Commented Mar 28, 2013 at 21:56
  • @user1318371 You've edited the code since I posted that comment. Commented Mar 28, 2013 at 21:57

2 Answers 2

1

You will only find a bad value if it is the last node you are checking against your list. You need to change your check loop to be something like this:

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;
    }

This stops the validation as soon as an item being checked isn't in the list.

Sign up to request clarification or add additional context in comments.

1 Comment

Sweet! You've been arrowed up!
1

There's so much code a lot of people (like me) won't even start reading it. Try writing a shorter program that reproduces the problem.

Also I suggest you read http://www.cplusplus.com/reference/string/string/compare carefully.

1 Comment

Code I am having a problem with has been commented.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.