Help Please

Having a problem with my code,

If I enter more than 1 customer with the same name the function only returns the most recent update. Am I missing anything?


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
90
  void add_new_customer()
{
	string first_name;
	string last_name;
	unsigned long long phone_no;
	string city;
	string size;
	string notes;

	ofstream newcustomer ("Customer List2.txt", ios::app); 
	
	cout<<"--------------------------"<<endl;
	cout<<"First Name:\t";
	cin>>first_name;
	cout<<"\nLast Name:\t";
	cin>>last_name;
	cout<<"\nPhone Number:\t";
	cin>>phone_no;
	cout<<"\nPrefered City:\t";
	cin>>city;
	cout<<"\nProperty Size:\t";
	cin>>size;
	cout<<"Customer Notes:\t\n";
	cin>>notes;

	newcustomer << first_name <<"  "<< last_name <<"   "<< phone_no <<"   "<<city<<"  "<<size<<"  "<< notes<<endl;
	newcustomer.close();
	
	main();










void search_customer_name()
{
	ifstream customer ("Customer list2.txt");
	string first_name;
	string last_name;
	unsigned long long phone_no;
	string city;
	string size;
	string notes;
	string input_first_name;
	string input_second_name;

	system("cls");
	cout<<"--------------------------"<<endl;
	cout<<"Search\n\n"<<endl;
	cout<<"First Name:\t";
	cin>>input_first_name;
	cout<<"--------------------------"<<endl;

	while (customer >>first_name>>last_name>>phone_no>>city>>size>>notes)
	{
		if (input_first_name == first_name)
		{
			system ("cls");
			cout<<"--------------------------"<<endl;
			cout<<"Customer(s) found"<<endl;
			cout<<"--------------------------"<<endl;
			cout<<"First Name\tSecond Name\tPhone Number\tCity\t\tProperty Size\t"<<endl;
			cout<<first_name<<"\t\t"<<last_name<<"\t\t"<<phone_no<<"\t"<<city<<"\t\t"<<size<<endl;
			cout<<"--------------------------"<<endl;
			system ("pause");
			main();

		}

		else if (!(input_first_name == first_name))
		{
			cout<<"--------------------------"<<endl;
			system ("cls");
			cout<<"No Customer(s) Match Your Search"<<endl;
			cout<<"--------------------------"<<endl;
		}
	}

	system("pause");
	cin.get();
	main();
	
	

	}
Yeah, if you want to find every customer with that name you have to go through the entire file instead of calling main to..

WHAT ARE YOU DOING CALLING MAIN!? YOU SHOULD NEVER CALL MAIN! EVER! Use return like a sensible human being!
Yeahh never call main(). You shouldn't even be able to, as the standard forbids it. Does your program ever even end?
I've removed main, yet i'm still having the same problem.

FYI this isn't the entire program just the two functions I figured caused the issue.

The data is saving to my Customer list2.txt without any issues.

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
void search_customer_name()
{
	ifstream customer ("Customer list2.txt");
	string first_name;
	string last_name;
	unsigned long long phone_no;
	string city;
	string size;
	string notes;
	string input_first_name;
	string input_second_name;

	system("cls");
	cout<<"--------------------------"<<endl;
	cout<<"Search\n\n"<<endl;
	cout<<"First Name:\t";
	cin>>input_first_name;
	cout<<"--------------------------"<<endl;

	while (customer >>first_name>>last_name>>phone_no>>city>>size>>notes)
	{
		if (input_first_name == first_name)
		{
			system ("cls");
			cout<<"--------------------------"<<endl;
			cout<<"Customer(s) found"<<endl;
			cout<<"--------------------------"<<endl;
			cout<<"First Name\tSecond Name\tPhone Number\tCity\t\tProperty Size\t"<<endl;
			cout<<first_name<<"\t\t"<<last_name<<"\t\t"<<phone_no<<"\t"<<city<<"\t\t"<<size<<endl;
			cout<<"--------------------------"<<endl;
			system ("pause");
			

		}

		else if (!(input_first_name == first_name))
		{
			cout<<"--------------------------"<<endl;
			system ("cls");
			cout<<"No Customer(s) Match Your Search"<<endl;
			cout<<"--------------------------"<<endl;
		}
	}

	system("pause");
	cin.get();
	
	
	

	}
It's probably because you're clearing the screen.

You find a name, print their information to the screen, and on the next pass through you clear the screen again.
Line 36: There is no need for the if here. else is adequate since you've already tested if they're the same (although this whole else if probably shouldn't be here, see below).

You don't want to display "No customer(s) match" for each record read that doesn't match. You want to keep a count of the number of matches found, then after the read loop, check the counter and display "No customer(s) match" if count is zero.


Topic archived. No new replies allowed.