C++/help me out
Expert: vijayan - 11/3/2011
QuestionQUESTION: thank you so much
i understand you
but can you help me out
i tryed to use vector function to solve the search function
but it doesnt display anything back
#include <sstream>
#include <vector>
#include <stdio.h>
int search()
{
{
std::string SearchID;
std::vector<std::string> searchWords;
while(true)
{
std::cout << "Please enter a search Word or phrase, empty line to quit";
std::cin >> SearchID;
if (SearchID.empty()) break;
searchWords.push_back(SearchID);
}
std::ifstream Searchfile( "document.txt" ) ;
std::string line ;
while( getline (Searchfile,line) )
{
for (int ix = 0; ix < searchWords.size(); ++ix)
{
if( line.find(searchWords[ix]) != std::string::npos )
{
std::cout << "found in line: "<< '\n' << line << '\n';
}
}
}
}return 0;
}
ANSWER: This should produce an output if the file was opened successfully and it contains one of the words.
Immediately after:
std::ifstream Searchfile( "document.txt" ) ;
Add
if( !Searchfile ) { std::cerr << "error opening file\n" ; }
---------- FOLLOW-UP ----------
QUESTION: your ARE RIGHT IT DOESNT OPEND THE TEXT DOCUMENT WHAT COULD BE wrong please help me
AnswerI can only guess; most probable cause would be that the file "document.txt" does not exist in the working directory of the program.
If it is elsewhere, copy it to the working directory.
Alternatively, use a fully qualified path instead of just a relative path.