Input and output from file

I have a input file named BabyNames.dat

The data are
1 James Ellen
2 Peter Eleanor
3 Rodger Mary
4 John Elise
5 Mpho Anne
6 Molefe Ella
7 Zaheer Petunia
8 Charles Eugenie
9 Tabang Charlotte
10 Michael Nazeera

Its Male - Female on rank 1 - 10
Basicly I need to find lets say A girl that starts with the Letter "E" then all the girls with starting letter "E" with their rank will be displayed. Same with boys.

If I want "m" then
it will say something like

5 Mpho
6 Molefe
10 Michael

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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include <iostream>

#include <string>

#include <fstream>

using namespace std;



int readfile(string boy[], string girl[]);

int search_boy(string boy[], string boy_name, int records);

int search_girl(string girl[], string girl_name, int records);

int main() {

               const int MAX = 1000;

               string name;

               int n=0;

               string boy_names[MAX], girl_names[MAX];

               n = readfile(boy_names,girl_names);


               cout<<" Enter the name to search : ";

               cin>>name;

               int rank = search_boy(boy_names,name,n);

               return 0;

}


int readfile(string boy[], string girl[])

{

               int rank, records=0;

               string girl_name, boy_name;

               ifstream fin("BabyNames.dat") ;

               if(fin.is_open())

               {



                              while(!fin.eof())

                              {

                                             fin>>rank>>boy_name>>girl_name;

                                             boy[records] = boy_name;

                                             girl[records] = girl_name;

                                             records++;

                              }

                              fin.close();

               }else

                              cout<<" Unable to open file : BabyNames.dat"<<endl;

               return records;

}



int search_boy(string boy[], string boy_name, int records)

{

               for(int i=0;i<records;i++)

               {

                              if(boy[i].compare(boy_name) == 0)

                                             return (i+1);

               }

               return -1;

}



int search_girl(string girl[], string girl_name, int records)

{

               for(int i=0;i<records;i++)

               {

                              if(girl[i].compare(girl_name) == 0)

                                             return (i+1);

               }

               return -1;

}
Perhaps:

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
#include <iostream>
#include <string>
#include <fstream>
#include <cctype>

int readfile(std::string boy[], std::string girl[]);
void search(const std::string names[], unsigned char flet, int records);

constexpr int MAX { 1000 };

int main() {
	unsigned char let {};
	std::string boy_names[MAX], girl_names[MAX];

	if (auto n { readfile(boy_names, girl_names) }; n > 1) {
		std::cout << "Enter the first letter of the name to search : ";
		std::cin >> let;
		let = static_cast<unsigned char>(std::toupper(let));

		std::cout << "\nBoys names starting with " << let << '\n';
		search(boy_names, let, n);

		std::cout << "\nGirls names starting with " << let << '\n';
		search(girl_names, let, n);
	}
}

int readfile(std::string boy[], std::string girl[]) {
	std::ifstream fin("BabyNames.dat");

	if (!fin)
		return -!!(std::cout << "Unable to open file : BabyNames.dat\n");

	int records {};

	for (int rank {}; records < MAX && (fin >> rank >> boy[records] >> girl[records]); ++records);
	return records;
}

void search(const std::string names[], unsigned char flet, int records) {
	for (int i {}; i < records; ++i)
		if (static_cast<unsigned char>(std::toupper(names[i][0])) == flet)
			std::cout << i + 1 << ' ' << names[i] << '\n';
}



Enter the first letter of the name to search : m

Boys names starting with M
5 Mpho
6 Molefe
10 Michael

Girls names starting with M
3 Mary

Last edited on
Currently search_boy(...)/search_girl(...) are returning a single index to the first found entry. For more than one entry you need to provide an array of indexes. In that case the return value would be the amount of names found.

Alternatively you could provide an array of found names:

int search_boy(string found_boy[], string boy[], string boy_name, int records) // returns the amount of found names

In this case you have a loop similar to readfile(...)

Note that compare(...) compares the whole name only. I suggest that you use the find(...) function. See:

https://cplusplus.com/reference/string/string/find/

By the way: You don't need two functions for girl/boy. You need only one where you provide the appropriate girl/boy array and records.
Topic archived. No new replies allowed.