If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register or Login
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Searching file for string

I'm in a beginner C++ course, and have gotten stuck on an assignment I've been given. This is the part I'm having trouble with right now:

The program then asks the user for a search string, which may contain white space. The program should search the file for every occurrence of the search string. When the string is found, the line that contains it should be displayed in the following format
nnnnn:Line Contents
That is the line number of the line, 5 columns wide, right justified, followed by a colon, followed by the contents of the line.

Re: Searching file for string

string::npos is a constant of type size_t, which essentially is nothing else than an unsigned int. So, your test whether the search string has been found could look like this:

Code:

found = text.find(toFind) != string::npos;

with the type of found changed to bool. However, since you're only using the found status stored in the variable in a single if condition anyway, you can eliminate the variable found altogether and do the check right inside the if instruction, like this:

Code:

if (text.find(toFind) != string::npos)
{
// ...
}

IMO that's not only shorter overall, but also clearer.

I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Re: Searching file for string

However, since you're only using the found status stored in the variable in a single if condition anyway, you can eliminate the variable found altogether and do the check right inside the if instruction, like this:

Code:

if (text.find(toFind) != string::npos)
{
// ...
}

IMO that's not only shorter overall, but also clearer.

I used this, but it still prints the entire file, not just the line where the text is found

Re: Searching file for string

Oh, okay, I realized my issue is that I was entering a two word phrase and it was only searching for the first word. How can I get it to accept the space character and search for phrases as well? I changed cin >> toFind to getline(cin, toFind), but it's still not working.

Re: Searching file for string

Exactly that should do the trick, and it does for me. I simply copy-pasted your proposed command from your post into my version of your program here. Could it be you made the change but then forgot to rebuild the program?

I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

But I can't get the search to repeat. It asks for the string again, but doesn't search again. It also doesn't exit immediately if the user types "//", they have to type it again before it will close. Why isn't the while (toFind != "//") loop working?

Re: Searching file for string

Originally Posted by taymaxi

But I can't get the search to repeat. It asks for the string again, but doesn't search again. It also doesn't exit immediately if the user types "//", they have to type it again before it will close. Why isn't the while (toFind != "//") loop working?

I think it's time that you started to debug your code, instead of just writing code, running it, and asking "what's wrong?" Debugging is part and parcel of learning how to write computer programs. It is a mandatory process.

What is the value of "toFind" when the while loop is entered? What is the flow of your program when you run it? If you single-stepped through the program with the debugger, where does the program flow take you? All of these questions are answered by debugging your code.