Question

I have 2 codes here, the first one here prompts you for a number, then tells you what is on that line number in the text file "example.txt"

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

using namespace std;

int main()
{
    string s;
    vector <string> v;
    ifstream fileInput;
    int qwe = 0;
    fileInput.open("example.txt");
    while (getline( fileInput, s ))
    {
        v.push_back( s );
    }
    cout << "number: " << endl;
    cin >> qwe;
    cout << "line " << qwe << ": " << v[ qwe ] << endl;
    fileInput.close();
}

and a second code here prompts the user for input then adds a "?" at the beginning because it's for my algorithm in the future, it will be used then. But then it searches for that in the text file and gives the user the line number of what the user inputted

#include <iostream>
#include <fstream>
#include <cstdio>
#include <cstring>
#include <sstream>
#include <string>

using namespace std;

int main()
{
    ifstream fileInput;
    int offset;
    string line;
    string search;

    cout << "Hi" << endl;
    getline(cin, search);
    search = "?" + search;
// open file to search
    fileInput.open("example.txt");
    if(fileInput.is_open())
    {
        while(getline(fileInput, line))
        {
            for(unsigned int curLine = 2; getline(fileInput, line); curLine++)
            {
                if (line.find(search) != string::npos)
                {

                    cout << "found: " << search << " line: " << curLine << endl;
                }
            }
        }
        fileInput.close();
    }
    else cout << "Unable to open file.";
}

So my problem is that I need to sort of combine these codes, I need it so that it prompts the user for input and then it figures out the line number, and then it couts the next line, how do I do this?

Was it helpful?

Solution

Like this would do:

#include <fstream>
#include <algorithm>
#include <iostream>

int main()
{
    std::string input;

    std::cout<<"Enter a line to search for: \n";
    std::getline(std::cin, input);

    std::fstream File("example.txt", std::ios::in);

    if (File.is_open())
    {
        std::string line;
        int line_count = 0;

        while(std::getline(File, line))
        {
            if (line.find(input) != std::string::npos)
            {
                std::cout<<"The line found was: \""<<line<<"\" at line: "<<line_count<<"\n";

                if (std::getline(File, line))
                {
                    std::cout<<"The line after that is: \""<<line<<"\"\n";
                    ++line_count;
                }
                else
                {
                    std::cout<<"There are no lines after that!\n";
                }
            }
            ++line_count;
        }

        File.close();
    }
}

With an example file of:

hello world
I am testing
finding lines

you can search for "hello" and it will return line 0 aka the first line..

However, if you turn on find_approximate_line and searched for "hey world", it will still return line 0 because of the HammingDistance algorithm.

If you don't care about partial/close matches then you can remove the HammingDistance algorithm and keep using the std::string.find.

One example output is:

Enter a line to search for:
> hello world

The line found was: "hello world" at line: 0
The line after that is: "I am testing"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top