-6

I have been struggling with comparing two strings which I read from files, "one" & "two" both have the same words (e.g. salt) but it doesn't return "Equal". I have also used == but it made no difference.

#include <iostream>
#include <cstring> 
#include <fstream>
using namespace std;

int main (){ 

    string en[100];
    string line;

    int i=0;
    ifstream fileEn ("hey.txt");
    if (fileEn.is_open()){
        while (!fileEn.eof()){
            getline(fileEn,line);
            en[i]=line;
            i++;
        }
    }
    fileEn.close();

    string fa[100];
    string line1;

    i=0;
    ifstream fileFa ("hey1.txt");
    if (fileFa.is_open()){
        while (!fileFa.eof()){
            getline(fileFa,line1);
            fa[i]=line1;
            i++;
        }
    }
    fileFa.close(); 

    ifstream Matn("matn.txt");
    string matn[100];
    if(Matn.is_open()){    
        for(int i = 0; i < 100; ++i){
            Matn >> matn[i];
        }
    }
    Matn.close();
    string one = en[0];
    string two = matn[0];
    cout << one << "  " << two;
    if(one.compare(two) == 0){
        cout << "Equal";
    }
}
Swifty
  • 839
  • 2
  • 15
  • 40
  • 4
    Use ==. Use a debugger. –  Jan 01 '18 at 22:47
  • Any or all of your i/o operations could fail, but you wouldn't know because you don't check. https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong `cstring` is also not the same header as `string`. How were the files written? What OS are you running this on? – Retired Ninja Jan 01 '18 at 22:52
  • You use different ways to read the strings (getline to read en, and >> to read matn) , so would check that one doesn't also have a newline in it which would make them different. E.g cout << "'" << one << "' '" << two << "'" << "\n" ; – Ian4264 Jan 01 '18 at 22:57
  • @Ian4264 "_would check that one doesn't also have a newline in it_" None of `getline` (as used in the sample), and `cin` would ever read a newline character. `getline` reads, but discards delimiter, and `cin` reads, and discards all whitespaces. – Algirdas Preidžius Jan 01 '18 at 23:31
  • 1
    `#include ` rather than ``. The two are VERY different. Using the second, strings cannot be compared for equality using `==` since the comparison is done of addresses, not contents. Assignment will also not do what you expect. – Peter Jan 02 '18 at 02:21
  • @Peter `std::string` can absolutely be compared with `==`. – Retired Ninja Jan 02 '18 at 10:10
  • @RetiredNinja - Yes, that is true. However, the question is using ``. None of the headers included actually are guaranteed to `#include `, therefore `string` in the code is not necessarily `std::string`. – Peter Jan 02 '18 at 11:34
  • @Peter I think it's a fair assumption since the usage of getline, insertion operators, and the compare function correspond with features of std::string, and `cstring` aka `string.h` doesn't define a string type. – Retired Ninja Jan 02 '18 at 11:43
  • @RetiredNinja - not really. The question concerned behaviour that would not occur with `std::string`. – Peter Jan 02 '18 at 11:52
  • @Peter I disagree. I believe the problem is the strings are in fact not equal, but the question doesn't include enough details to determine why that is. – Retired Ninja Jan 02 '18 at 13:56

1 Answers1

1

I suggest adding some debugging output to your program:

    while (!fileEn.eof()){
        getline(fileEn,line);

        // Debugging output
        std::cout << "en[" << i << "] = '" << line << "'" << std::endl;

        en[i]=line;
        i++;
    }

and

    for(int i = 0; i < 100; ++i){
        Matn >> matn[i];

        // Debugging output
        std::cout << "matn[" << i << "] = '" << matn[i] << "'" << std::endl;

    }

Hopefully you can see what the problem is by looking at the output.


In addition, please note that use of while (!fileEn.eof()){ ... } is not correct. See Why is iostream::eof inside a loop condition considered wrong?.

I suggest changing that loop to:

    while (getline(fileEn,line)) {

        // Debugging output
        std::cout << "en[" << i << "] = '" << line << "'" << std::endl;

        en[i]=line;
        i++;
    }

Similarly, don't assume that Matn >> matn[i] is successful. I suggest changing that loop to:

    for(int i = 0; i < 100; ++i) {
        std::string s;
        if ( !(Matn >> s) )
        {
           // Read was not successful. Stop the loop.
           break;
        }

        matn[i] = s;

        // Debugging output
        std::cout << "matn[" << i << "] = '" << matn[i] << "'" << std::endl;

    }
R Sahu
  • 204,454
  • 14
  • 159
  • 270