Search Function
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Search Function

  1. #1
    Join Date
    Dec 2013
    Posts
    2

    Search Function

    've been looking for so long trying to figure out how to get this thing to work. All i need to do is ask user to input a name and then it brings out the line from the .txt file containing the information.

    For example in my case I'm doing a member search function I'm required to ask user to input the name of the customer and then print out all the details (which consumes 1 text line in the .txt file)

    Here is the code, any help would be greatly.. GREATLY appreciated!

    This is the write to text file method (100% working)


    Code:
    cout << "Customer Name: ";
    cin >> name; 
    
    // ...
    
    ofstream myfile("customer.txt", ios::app);
    
    // ...
    
    myfile << " ID: " << id << " Name: " << name << " NICN : " << nicn << " gender : " << gender << " Day : " << day 
           << " Month : " << month << " Year : " << year << " Status : " << status << " Charges : " << charges << endl;
    My search function..:

    Code:
    std::string search_customer(const std::string& name, const char* path = "customer.txt")
    {
    
    	cout << "enter name: ";
    	getline(cin, name);
    
    	std::ifstream myfile(path);
    	std::string line;
    
    	while (getline(myfile, line))
    	{
    		std::istringstream stm(line); // create an input string stream which reads fronm the line
    
    		std::string token;
    		while (stm >> token && token != name_prefix); // read upto, and including, prefix
    
    		std::string str_name, str_suffix; // the next token is the name
    		if (stm >> str_name && str_name == name && stm >> str_suffix && str_suffix == name_sufffix)
    			return line;
    	}
    
    	std::cerr << "look up of name failed\n";
    	return "";

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,012

    Re: Search Function

    So what's your question?
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  3. #3
    Join Date
    Dec 2013
    Posts
    2

    Re: Search Function

    Quote Originally Posted by D_Drmmr View Post
    So what's your question?
    When I access the search function it always displays

    look up of name failed.

    Even if the output was supposed to be positive.

    My question is how to make it to function properly?

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,308

    Re: Search Function

    Have you traced through the code using the debugger to see where it is departing from what you expected from your design?

    Where is name_suffix and name_prefix defined?
    Last edited by 2kaud; December 8th, 2013 at 07:05 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center