I have been browsing the web for days now trying to find a solution to my problem and have yet to come up with anything so far so I figured I would ask some professionals.

Basically I need to read a text file which contains records along the lines of:

ClientNum LName FName
123456 XX YY
etc..
etc..

and then let the user input either a range of ClientNum's or one specific ClientNum and display all data for that one record. So far I have been using getline and have successfully displayed all records with a while loop but I cannot figure out the search function. By the way I need to use the BASICS of C++ so basically just loops. I will post my code for you to review and let me know if you can help out thanks!

#include <iostream>
#include <fstream>

using namespace std;


int main()
{
   string getcontent;
   string search;
  
   cout << "Input: ";
   cin >> search;
   
    ifstream openfile ("TEST.txt");
    
     if(openfile.is_open())
     {

        while(!openfile.eof())
        {                   
          getline (openfile,getcontent);//
          cout << get content << endl; // these two lines alone in a while loop display ALL records
          
          
        
        if(search == getcontent ) // this was my theory for searching records but does not display anything to the console
         {
        cout << getcontent << endl;
         }
    
        }
        openfile.close();
    
     }
     else
         {
         cout << "File error\n";
         break;
         }
    

    
    
    
    
    system("PAUSE");
    return 0;
}

In order to perform operations on your data, you need to load it in the memory in way it is convenient for you to use. If I were you, I would make a structure or class for that purpose - which can hold the data of one record. Then I would make an "array" of the structure, so I could store all the data from the file. I would use vector , or list from the stl , if you need to insert and remove elements frequently choose list, otherwise vectors are fine. Then read all the data from the file into the vector/list.

#include <vector>
#include <fstream>
#include <string>
#include <sstream>

struct Record
{
	std::string name, address;
	int client_number;
        /*
        .
        other stuff you need to store
        .
        */
};

bool init ( std::vector<Record>& );

int main ()
{
	std::vector<Record>clients;

	if ( ! init ( clients )  ) return -1;
	// if it fails to open the file the program returns with -1
	// since you wouldn't have any data to work with, there is no point
        // running the program any further, or you can check the size of the vector

	/*
	.
	. other function callings
	.
	*/

	return 0;
}

bool init ( std::vector<Record> &v )
{
    Record tmp; // to store each record
    std::ifstream in ( "TEST.txt" );
    std::string line;
    std::stringstream ss;  // to manipulate the string

    if ( !in ) return false;

    while ( getline ( in, line ) )
    {
        // I dont know exactly how  your file is arranged
        // but there must be a deliminator character somewhere between the records.
        // If not, count the lines you've read, and make a check each time you are
        // reading a new record. You can ensure you are reading the right data
        // by checking the line. - all records start with "ClientNum"

        // and depending on who your file looks like, you can extract the string with stringstream
        // ss << line;
        // ss >> tmp.client_number;
        // ss >> tmp.name;
        // or you can use getline ( ss, tmp.name, '\n' ); dependig who you want to extract the stream
        // etc.
        v.push_back ( tmp ); // and finally push back the data into the vector
    }

    in.close ();

    return true; // all went good, return true
}

After you have read all the data into the vector, you can work on it comfortably and implement all your functions.

Thank you for your help! I used a string function .find() to read the client number

helo ma'am i have some issue.
i wanna search data in c++using handling if u help me

commented: Bury your new request under 8 years of discussion isn't the way. Start a new discussion. -3
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.