Thread: comparing two string arrays

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    comparing two string arrays

    Hello everyone,

    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:
    Code:
    //
    //  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 (strg[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:
    Code:
    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!

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    One of your problems will be found in this snippet:
    Code:
            int numberOfNodes = convertToInt(fileData);
    
            string list[numberOfNodes];
    In C++ when declaring arrays the size of the array must be a compile time constant. Why are you using arrays instead of std::vector?

    Next if your file looks like:
    Code:
    16
    Cape Birmingham Boston Chicago Dallas Detroit KansasCity LosAngeles Memphis Minneapolis Omaha Orlando Richmond SanFrancisco Seattle StLouis
    Atlanta Chicago 718
    Why are you using getline() to retrieve the first entry? Using the extraction operator>> to place the value in an int would make more sense.

    Next since your strings don't seem to have any spaces in them it may also be easier to use the extraction operator to retrieve your strings instead of getline().

    Have you tried printing out your various arrays so you can insure you are properly reading your file?

    Also running the program thru your debugger may also help you figure out your problem.

    Jim

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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'm really curious why.

    What happens to the number on the end of your lines?

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by jimblumberg View Post
    One of your problems will be found in this snippet:
    Code:
            int numberOfNodes = convertToInt(fileData);
    
            string list[numberOfNodes];
    In C++ when declaring arrays the size of the array must be a compile time constant. Why are you using arrays instead of std::vector?

    Next if your file looks like:
    Code:
    16
    Cape Birmingham Boston Chicago Dallas Detroit KansasCity LosAngeles Memphis Minneapolis Omaha Orlando Richmond SanFrancisco Seattle StLouis
    Atlanta Chicago 718
    Why are you using getline() to retrieve the first entry? Using the extraction operator>> to place the value in an int would make more sense.

    Next since your strings don't seem to have any spaces in them it may also be easier to use the extraction operator to retrieve your strings instead of getline().

    Have you tried printing out your various arrays so you can insure you are properly reading your file?

    Also running the program thru your debugger may also help you figure out your problem.

    Jim
    Not really sure what any of these comments have to do with the problem I am having. Yes I have printed the arrays. Thank you for your time.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by whiteflags View Post
    I'm really curious why.
    Please read the code:
    Code:
    if (check == 0) {
                cout << "Incorrect DataFile!\nStrings listed should match Node Strings" << endl;
                return;
    What happens to the number on the end of your lines?
    That is taken care of through another function not listed here.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Not really sure what any of these comments have to do with the problem I am having.
    I'm trying to point out that you are doing things the hard way, there are much easier ways of reading your file.

    What exactly is your problem?

    Your arrays won't match exactly because the data is not the same.


    Jim

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by jimblumberg View Post
    I'm trying to point out that you are doing things the hard way, there are much easier ways of reading your file.

    What exactly is your problem?

    Your arrays won't match exactly because the data is not the same.


    Jim
    Sure they will, If I change the first city "Cape", in the second line of the file to "Atlanta", all the nodes will match with data in the edge list, hence no error code of:
    Code:
    if (check == 0) {
                cout << "Incorrect DataFile!\nStrings listed should match Node Strings" << endl;
                return;
            }
    If I leave the city "Cape", I should get an error code. the problem is I am NOT getting an error code because my attempts to compare the arrays are incorrect. That is where I am looking for the help. In the code above I comment where my problem, I believe, starts and stops.

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Problem solved. The following code needed to be changed from:
    Code:
    for (i = 0; i < y; i++) {               //<- my problem starts here
                check = 0;
                for (j = 0; j < x; j++) {
                    if (strg[i].compare (strg[j]) == 0) {
                        check = 1;                  //<- my problem ends here
                        break;
                    }
                }
            }
            if (check == 0) {
                cout << "Incorrect DataFile!\nStrings listed should match Node Strings" << endl;
                return;
            }
    to:
    Code:
    for (i = 0; i < y; i++) {
                check = 0;
                for (j = 0; j < x; j++) {
                    if (strg[i].compare (list[j]) == 0) {
                        check = 1;
                        break;
                    }
                }
                if (check == 0) {
                cout << "Incorrect DataFile!\nStrings listed should match Node Strings" << endl;
                break;
                }
            }

  9. #9
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Code:
    ...
    while (!file.eof()){
        getline (file, fileData);
        stringstream ss (fileData);
    ...
    Read: FAQ > Why it's bad to use feof() to control a loop
    The example in the link above uses an example in C but the lesson still applies to C++. You should test the read operation (the getline) in the while loop's conditional instead of an end-of-file test.

    Code:
    ...
    while (getline (file, fileData)){
        stringstream ss (fileData);
    ...
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Just pointing out a few things...
    - fstream has a constructor. Consider using it.
    - There are also C++ arrays, called std::array which integrate better into the language. It may be worth taking a look at them.
    - There is no need to use .compare() to compare two strings. Simply use relational operators such as ==, < and >. This isn't Java.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I need help comparing arrays
    By Guy_from_SoCal in forum C++ Programming
    Replies: 1
    Last Post: 11-10-2012, 12:30 AM
  2. Multidimensional string arrays and comparing characters
    By ArcadeEdge in forum C Programming
    Replies: 4
    Last Post: 02-15-2012, 05:37 PM
  3. Comparing string arrays
    By never_lose in forum C Programming
    Replies: 17
    Last Post: 03-20-2011, 01:38 AM
  4. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:17 AM
  5. Comparing Arrays
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-23-2001, 08:07 PM