Search code examples
c++stringfstream

Check all lines in the file that have both, one or none of the strings in the same line


I want to check if the chosen strings appear in the same line. Sadly I don't get the correct output. The file contains this text

/* one 
two
one two
two one
two 
one
something
else */



#include <iostream>
#include <string>
#include <fstream>

using namespace std;

void display(ifstream& MyFile, string a, string b)
{
    string s="";
    int choice;
    cout << "Choose the mode you want to use " << endl;
    cout << "1. Find both words" << endl;
    cout << "2. Find one words" << endl;
    cout << "3. Find where there is none words" << endl;
    cout << "Choose the mode you want to use : ";
    cin >> choice;
    switch (choice) {
    case 1:
        while (getline(MyFile, s))
        {
    
            if (s.find_first_of(a, 0) && (s.find_first_of(b, 0)) )
            {
                cout << "Found both words" << endl;  
            }
        }
        break;
    case 2:
        while (getline(MyFile, s))
        {
            if ((s.find_first_of(a, 0)  && !s.find_first_of(b, 0)) || (!s.find_first_of(a, 0) && s.find_first_of(b, 0) ))
            {
                cout << "Found one word" << endl;
            }
        }
        break;
    case 3:
        while (getline(MyFile, s))
        {
            getline(MyFile, s);
            if (!s.find_first_of(a, 0) && !s.find_first_of(b, 0))
            {
                cout << "No words " << endl;
            }
        }
        break;
    default:
        cout << "Wrong Input" << endl;
    }
}

  
int main()
{
    ifstream Myfile("Mydata.dat");
    string a = "one", b = "two",fileData;
    display(Myfile, a, b);
    Myfile.close();
    return 0;
}

I get "Found both words" 5 times whereas it should be 2. The same thing happens with the other options. My thoughts are that in the if statement I compare the function s.find_firstof(str, pos) wrongly because this function returns the index of the first instance in s of any character in str, starting the search at position pos. My second thought is that I read the data of the file wrong.

Found both words
Found both words
Found both words
Found both words
Found both words

Solution

  • You are doing the if statement wrong.

    I changed the way to verify if a string contains another one, basically s.find(a) will return the start position of a in s if it exists in it, or will return -1 if it doesn't. This way, verifying like this s.find(a) != -1 or s.find(a) <s.length() would work the same way, bacause in conversion of -1 to int is the max of size_t

    This should solve your problem:

    #include <iostream>
    #include <string>
    #include <fstream>
    
    using namespace std;
    
    void display(ifstream &MyFile, string a, string b)
    {
        string s = "";
        int choice;
        cout << "Choose the mode you want to use " << endl;
        cout << "1. Find both words" << endl;
        cout << "2. Find one words" << endl;
        cout << "3. Find where there is none words" << endl;
        cout << "Choose the mode you want to use : ";
        cin >> choice;
        switch (choice)
        {
        case 1:
            while (getline(MyFile, s))
            {
    
                if (s.find(a) < s.length() && (s.find(b) < s.length()))
                {
                    cout << "Found both words" << endl; // means that this line have BOTH words
                }
            }
            break;
        case 2:
            while (getline(MyFile, s))
            {
                if (s.find(a) < s.length() || (s.find(b) < s.length()))
                {
                    cout << "Found one word" << endl; //means that this line have at least one of the two words
                }
                /*
                if (s.find(a) < s.length() && (s.find(b) < s.length())) 
                {
                    cout << "Found one word" << endl; //means that this line have the two words at the same time
                }
                */
            }
            break;
        case 3:
            while (getline(MyFile, s))
            {
                getline(MyFile, s);
                if (!s.find_first_of(a, 0) && !s.find_first_of(b, 0))
                {
                    cout << "No words " << endl; // means that this line do not have ANY of the two words
                }
            }
            break;
        default:
            cout << "Wrong Input" << endl;
        }
    }
    
    int main()
    {
        ifstream Myfile("Mydata.dat");
        string a = "one", b = "two", fileData;
        display(Myfile, a, b);
        Myfile.close();
        return 0;
    }
    

    The output ou this code is:

    Choose the mode you want to use 
    1. Find both words
    2. Find one words
    3. Find where there is none words
    Choose the mode you want to use : 1
    Found both words
    Found both words
    

    and

    Choose the mode you want to use 
    1. Find both words
    2. Find one words
    3. Find where there is none words
    Choose the mode you want to use : 2
    Found one word
    Found one word
    Found one word
    Found one word
    Found one word
    Found one word