Search Function

Hey all.. I'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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
void register_customer()
{
	static int id;
	int nicn, day, month, year;
	string name;
	char gender;
	bool status;
	double charges;

	cout << "Register new customer to the system!" << endl << endl;

	
	for (int i = id; i >= id; i++)
	{
		cout << "Customer ID: ";
		cin >> id;
	}
	
	

	cout << "Customer Name: ";
	cin >> name;

	cout << "NICN: ";
	cin >> nicn;

	
	
	do
	{
		cout << "Gender ('m' for male 'f' for female): ";
		cin >> gender;
	}


	while (gender != 'm' && gender !='f');
	



	cout << "Day of Birth ( 1 - 31 only): ";
	cin >> day;
	while (day < 1 || day > 31)
	{
		cout << "Invalid format!";
		cout << "Day of Birth ( 1 - 31 only): ";
		cin >> day;
	}

	cout << "Month of Birth ( 1 - 12 only): ";
	cin >> month;
	while (month < 1 || month > 12)
	{
		cout << "Invalid format!";
		cout << "Date of Birth ( 1 - 12 only): ";
		cin >> month;
	}

	cout << "Year of Birth ( 1935 - 2001 only): ";
	cin >> year;
	while (year < 1935|| year < 2014)
	{
		cout << "Invalid format!";
		cout << "Year of Birth ( 1935 - 2001 only): ";
		cin >> year;
	}

	cout << "Customer's Status ('0' for active '1' for banned) :";
	cin >> status;
	while (status !=1 && status != 0)
	{
		cout << "Please choose the right format.";
		cout << "Customer's Status ('0' for active '1' for banned) :";
		cin >> status;
	}

		cout << "Charges :";
		cin >> charges;


		ofstream myfile("customer.txt", ios::app);
		if (myfile.is_open())
		{
			myfile << " ID: " << id << " Name: " << name << " NICN : " << nicn << " gender : " << gender << " Day : " << day << " Month : " << month << " Year : " << year << " Status : " << status << " Charges : " << charges << endl;
			myfile.close();
		}

		else cout << "Unable to open file";
	}



and based on it I have created the search function which is where I'm having a headache..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void search_customer()
{
	string line;
	ifstream myfile("customer.txt");
	if (myfile.is_open())
	{
		(getline(myfile, line));
		
			cout << line << endl;
		
		myfile.close();
	}

	else cout << "Unable to open file";

}
From what I understand, this should help:
http://www.cplusplus.com/reference/string/string/find/
Nope it prints out everything in the file or just the first line in general.. I want it to print only based on user input for example the system will ask user to input name and then it will display the line saved on the customer.txt holding that name, hope it's a bit more clear now :)
Assuming that you have wriiten into the file with:
1
2
3
4
5
6
7
8
9
10
11
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;

something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>
#include <string>
#include <sstream> // ***

const char name_prefix[] = "Name:" ;
const char name_sufffix[] = "NICN :" ;

std::string search_customer( const std::string& name, const char* path = "customer.txt" )
{
	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 "" ;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <fstream>
#include <string>
#include <sstream> // ***

const char name_prefix[] = "Name:" ;
const char name_sufffix[] = "NICN :" ;

std::string search_customer( const std::string& name, const char* path = "customer.txt" )
{
	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 "" ;
}


Only problem left now is how to prompt the user to write the name of the user to search.. I tried doing this but didn't work. Any suggestions?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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 "";
}


ETA: Considering I'm also new to programming.. it would be really helpful to tell me how can I call the method in main.. thank you and sorry for my silly questions :D
Last edited on
Topic archived. No new replies allowed.