You are here:

C++/help me out

Advertisement


Question
QUESTION: 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

Answer
I 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.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


vijayan

Expertise

my primary areas of interest are generic and template metaprogramming, STL, algorithms, design patterns and c++11. i would not answer questions about gui and web programming.

Experience

about 15 years or so

Education/Credentials
post graduate engineer

©2016 About.com. All rights reserved.