Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So, this is the program i tried to write- The user will be given a chance to enter a string and the string will be matched with the string stored in a text file. When, i tried it, it worked fine and showed no problems. Now, i wanted to twist the program a bit and wanted to give the user multiple chances to search for a word from the database. This is where the program started showing me errors. I placed the program in a loop and tried to give the user two times to search for a word in the database. The problem is when i run the program, it only works for the first time the second time and the remaining times it doesn't search for the word in the database. I don't know where the problem is. Please can anyone suggest me anything. Here is my code.
My text file which i am looking for contains the following (Hello World, Hey).

C++
#include <iostream>
#include <fstream>
#include <string>

using namespace std;


int main()
{

  string search,line;
  ifstream read;

  int i =0;

  while(i<2)
  {
    read.open("type.txt");

  cout<<"Enter the word to search"<<endl;
  cin>>search;

  size_t pos;
  if(read.is_open())
  {
	  while (getline(read,line))
	  {
		   pos = line.find(search);
			   if(pos != string::npos)
			  {
				  

				  cout<<line<<endl;
				  
				  

			   }
	  }

	  




	  }

  i++;



  }







  system("pause");







  }
Posted
Comments
PIEBALDconsult 23-Nov-14 17:38pm    
Maybe try closing the file?
Surajit Das 23-Nov-14 18:15pm    
@PIEBALDconsult- awesome thanks it worked.!

The while loop should test if the ifstream is at eof or an error using good(). getline only returns the file stream not a boolean expression.

int main()
{
string _search;
string line;
ifstream read;

int i =0;
    while(i++<2)
    {
        cout<<"Enter the word to search"<<endl;
        cin>>_search;
        read.open("type.txt");
        size_t pos,i=0;
        if(read.is_open())
        {
            while (read.good())
            {
                i++;
                getline(read,line);
                pos = line.find(_search);
                if(pos != string::npos)
                {
                   cout<<i<<": "<<line<<endl;
                }
                else
                {
                    cout<<"Not found"<<endl;
                }

            }
            read.close();
        }

    }
}
 
Share this answer
 
C++
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
 

int main()
{
 
  string search,line;
  ifstream read;
 
  int i =0;
 
  while(i<2)
  {
    read.open("type.txt");
 
  cout<<"Enter the word to search"<<endl;
  cin>>search;
 
  size_t pos;
  if(read.is_open())
  {
	  while (getline(read,line))
	  {
		   pos = line.find(search);
			   if(pos != string::npos)
			  {
				  
 
				  cout<<line<<endl;
				  
				  read.close();
 
			   }
	  }
 
	  
 

 

	  }
 
  i++;
 

 
  }
 

 

 

 
  system("pause");
 

 

 

 
  }
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 23-Nov-14 18:46pm    
Not an answer. You got a solution from a member, but why posting an answer and even self accepting it?
—SA
Surajit Das 23-Nov-14 18:59pm    
@SA:- All i did was just updated the solution such that it can help somebody in the future. The member who suggested me the idea was a comment so i couldn't accept his solution. Regarding accepting my solution, it was just for fun!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900