Search a dynamic array for a string, and return the indices

Hello. I am looking for help with my program. I need it to search a dynamic array which I build from an input file. When it finds the user-input string, I want it to store the line number, and continue searching, and record all lines that contain the user-input string.

Here is a link to my complete main.cpp, header file, and implementation file. The function I am having trouble with is "Bagger::lineSearch"

http://codepad.org/azEKjpQ4

Thanks.
What is the matter? You can't record more than one number? You can't create the code to find the strings?
Looks like you need to increase your compiler warning levels, and fix all the warnings.
main.cpp|11|warning: ‘class Bagger’ has pointer data members [-Weffc++]|
main.cpp|11|warning: but does not override ‘Bagger(const Bagger&)’ [-Weffc++]|
main.cpp|11|warning: or ‘operator=(const Bagger&)’ [-Weffc++]|
main.cpp||In constructor ‘Bagger::Bagger()’:|
main.cpp|43|warning: ‘Bagger::key’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|43|warning: ‘Bagger::array_size’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|43|warning: ‘Bagger::arr’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|43|warning: ‘Bagger::in’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|43|warning: ‘Bagger::lineNumber’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|43|warning: ‘Bagger::index’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|43|warning: ‘Bagger::linesAdded’ should be initialized in the member initialization list [-Weffc++]|
main.cpp||In member function ‘void Bagger::addLines(int&)’:|
main.cpp|83|warning: declaration of ‘index’ shadows a member of 'this' [-Wshadow]|
main.cpp||In member function ‘void Bagger::lineNum(int&)’:|
main.cpp|102|warning: declaration of ‘index’ shadows a member of 'this' [-Wshadow]|
main.cpp||In member function ‘void Bagger::lineSearch(std::string, int&)’:|
main.cpp|126|warning: declaration of ‘index’ shadows a member of 'this' [-Wshadow]|
main.cpp|126|warning: declaration of ‘key’ shadows a member of 'this' [-Wshadow]|
||=== Build finished: 0 errors, 14 warnings (0 minutes, 1 seconds) ===|


Next look at this snippet:
1
2
3
4
5
6
7
void Bagger::openWebLog()  // Simply opens the weblog file
{
    // Precondition: None
    // Postcondition: The weblog file is opened and
    // ready to be processed.
    in.open("weblog_medium.txt");
}


Your comments are incorrect.
The "Precondition" should be "File must exist."

You should also be checking that the file properly opened and throwing some kind of error if it doesn't. Since a lot of your other functions depend on this file being opened you may want to consider opening the file in the constructor, and throwing an exception if it doesn't open properly.

Topic archived. No new replies allowed.