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

    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)

    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..:

    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

    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

    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
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012

    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. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++17 Compiler: Microsoft VS2017 (15.7.3)

Posting Permissions

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

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.