Printing out String (Struggling)

I'm honestly struggling on this program. I'm only trying to do one function where I must print out all of the friend's last and last names. The shell of the program is right. Nothing is wrong with the shell, it's just the functions I'm struggling with.

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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
  #include<iostream>
using namespace std;
#include<fstream>
#include<string>
void menu(int &selection);
void populate(string firstName[], string lastName[], int birthYear[], int birthMonth[], int birthDay[], char sex[], int age[], string dayBorn[], string season[], int &size);
void addFriend(string firstName[], string lastName[], int birthYear[], int birthMonth[], int birthDay[], char sex[], int age[], string dayBorn[], string season[], int size);
void deleteFriend(string firstName[], string lastName[], int birthYear[], int birthMonth[], int birthDay[], char sex[], int age[], string dayBorn[], string season[], int size);
void printFriends(string firstName[], string lastName[], int birthYear[], int birthMonth[], int birthDay[], char sex[], int age[], string dayBorn[], string season[], int size);
void printAFriend(string firstName[], string lastName[], int birthYear[], int birthMonth[], int birthDay[], char sex[], int age[], string dayBorn[], string season[], int size);
void printOldest(string firstName[], string lastName[], int birthYear[], int birthMonth[], int birthDay[], char sex[], int age[], string dayBorn[], string season[], int size);
void printYoungest(string firstName[], string lastName[], int birthYear[], int birthMonth[], int birthDay[], char sex[], int age[], string dayBorn[], string season[], int size);
void printAverageAge(int age[], int size);
void printMales(string firstName[], string lastName[], int birthYear[], int birthMonth[], int birthDay[], char sex[], int age[], string dayBorn[], string season[], int size);
int computeAge(int bYear, int bMonth, int bDay);
string computeSeason(int bYear, int bMonth, int bDay);
string computeDayBorn(int byear, int bmonth, int bday);

void quit();
const int MAXSIZE = 100;
int main()
{
	string firstName[MAXSIZE], lastName[MAXSIZE];
	int birthYear[MAXSIZE], birthMonth[MAXSIZE], birthDay[MAXSIZE];
	char sex[MAXSIZE];
	string dayBorn[MAXSIZE], season[MAXSIZE];
	int age[MAXSIZE];
	int selection;
	int size;
	populate(firstName, lastName, birthYear, birthMonth, birthDay, sex, age, dayBorn, season, size);

	do
	{
		menu(selection);
		if (selection == 1)
			addFriend(firstName, lastName, birthYear, birthMonth, birthDay, sex, age, dayBorn, season, size);
		else if (selection == 2)
			deleteFriend(firstName, lastName, birthYear, birthMonth, birthDay, sex, age, dayBorn, season, size);
		else if (selection == 3)
			printFriends(firstName, lastName, birthYear, birthMonth, birthDay, sex, age, dayBorn, season, size);
		else if (selection == 4)
			printAFriend(firstName, lastName, birthYear, birthMonth, birthDay, sex, age, dayBorn, season, size);
		else if (selection == 5)
			printOldest(firstName, lastName, birthYear, birthMonth, birthDay, sex, age, dayBorn, season, size);
		else if (selection == 6)
			printYoungest(firstName, lastName, birthYear, birthMonth, birthDay, sex, age, dayBorn, season, size);
		else if (selection == 7)
			printAverageAge(age, size);
		else if (selection == 8)
			printMales(firstName, lastName, birthYear, birthMonth, birthDay, sex, age, dayBorn, season, size);
		else if (selection == 0)
			quit();
		else
			cout << "Invalid selection " << endl;
	} while (selection != 0);
	
	system("pause");
	return 0;
}
void menu(int &selection)
{
	cout << "Welcome to My Friend Book " << endl << endl;
	cout << "1: Add a Friend " << endl;
	cout << "2: Delete a Friend " << endl;
	cout << "3: Print all Friends " << endl;
	cout << "4: Print a Friend" << endl;
	cout << "5: Print oldest Friend" << endl;
	cout << "6: Print youngest Friend" << endl;
	cout << "7: Print average age of friends " << endl;
	cout << "8: Print all male friends" << endl;
	cout << "0: quit" << endl;
	cin >> selection;
}



void populate(string firstName[], string lastName[], int birthYear[], int birthMonth[], int birthDay[], char sex[], int age[], string dayBorn[], string season[], int &size)
{
	ifstream infile;
	infile.open("friends.dat");
	size = 0;
	while(!infile.eof())
	{
	infile >> firstName[size] >> lastName[size] >> birthYear[size] >> birthMonth[size] >> birthDay[size] >> sex[size];

	}
}

void quit()
{
	cout << "good bye " << endl;
}

void addFriend(string firstName[], string lastName[], int birthYear[], int birthMonth[], int birthDay[], char sex[], int age[], string dayBorn[], string season[], int size)
{
	cout << "Function not implemented as yet " << endl;
}

void deleteFriend(string firstName[], string lastName[], int birthYear[], int birthMonth[], int birthDay[], char sex[], int age[], string dayBorn[], string season[], int size)
{
	cout << "Function not implemented as yet " << endl;
}

void printFriends(string firstName[], string lastName[], int birthYear[], int birthMonth[], int birthDay[], char sex[], int age[], string dayBorn[], string season[], int size)
{
	for (int i = 0; i < firstName[0]; i++)
	{
		for (int j = 0; j < lastName[0]; j++)
		{
		cout << firstName[i] << " " << lastName[j];	
		}
		cout << endl;
	}
	//cout << firstName[0] << " " << lastName[0] << endl;
	//cout << "Function not implemented as yet " << endl;
}

void printAFriend(string firstName[], string lastName[], int birthYear[], int birthMonth[], int birthDay[], char sex[], int age[], string dayBorn[], string season[], int size)
{
	cout << "Function not implemented as yet " << endl;
}


void printOldest(string firstName[], string lastName[], int birthYear[], int birthMonth[], int birthDay[], char sex[], int age[], string dayBorn[], string season[], int size)
{
	cout << "Function not implemented as yet " << endl;
}

void printYoungest(string firstName[], string lastName[], int birthYear[], int birthMonth[], int birthDay[], char sex[], int age[], string dayBorn[], string season[], int size)
{
	cout << "Function not implemented as yet " << endl;
}


void printAverageAge(int age[], int size)
{
	cout << "Functio not implemented as yet " << endl;
}

void printMales(string firstName[], string lastName[], int birthYear[], int birthMonth[], int birthDay[], char sex[], int age[], string dayBorn[], string season[], int size)
{
	cout << "Function not implemented as yet " << endl;
}

int computeAge(int bYear, int bMonth, int bDay)
{
	int age = 0;
	cout << "Function not yet implemented " << endl;
	//You should prompt the user for the current year, current month and current day
	return age;
}

string computeSeason(int bYear, int bMonth, int bDay)
{
	string season = "";
	cout << "Function not yet implemented " << endl;
	return season;
}


string computeDayBorn(int bYear, int bMonth, int bDay)
{
	    int d = bDay;
		int m = bMonth;
		
		int y = bYear % 100;
		int c = bYear / 100;
		if (m == 1 || m == 2)
		{
			m += 12;
			y--;
		}

		int D = (d + (m + 1) * 26 / 10 + y + y / 4 + c / 4 + 5 * c) % 7;
		if (D == 0)	return "Saturday";
		else if (D == 1)return "Sunday";
		else if (D == 2)return "Monday";
		else if (D == 3)return "Tuesday";
		else if (D == 4)return "Wednesday";
		else if (D == 5)return "Thursday";
		else if (D == 6)return "Friday";
		return "impossible";
		
}

1
2
3
4
5
6
7
8
9
10
11
12
13
void printFriends(string firstName[], string lastName[], int birthYear[], int birthMonth[], int birthDay[], char sex[], int age[], string dayBorn[], string season[], int size)
{
	for (int i = 0; i < firstName[0]; i++)
	{
		//for (int j = 0; j < lastName[0]; j++)
		//{
		cout << firstName[i] << " " << lastName[/*j*/i];	
		//}
		cout << endl;
	}
	//cout << firstName[0] << " " << lastName[0] << endl;
	//cout << "Function not implemented as yet " << endl;
}
That produces an error saying
[Error] no match for 'operator<' (operand types are 'int' and 'std::string {aka std::basic_string<char>}')
Oops sorry that’s 'i < size'
Is it the same for j? j < size?
Don’t you know about struct or class yet?
Without a sample of your data it is hard to know where the bugs hide.
For example, in populate() prototype you declare 10 parameters:
1
2
3
4
5
6
7
8
9
10
void populate (std::string firstName[],
               std::string lastName[],
               int birthYear[],
               int birthMonth[],
               int birthDay[],
               char sex[],
               int age[],
               std::string dayBorn[],
               std::string season[],
               int& size);

but you use only 6 of them:
1
2
3
4
5
6
infile >> firstName[size]
       >> lastName[size]
       >> birthYear[size]
       >> birthMonth[size]
       >> birthDay[size]
       >> sex[size];

(well, you also use size, but in my opinion it would be clearer to return it).

Is this an assignment? Are you sure you can’t use structs? You teacher should be a sadistic to give you such an assignment without having introduced structs!

About the same line of code:
1
2
3
4
5
6
7
8
9
10
size = 0;
while(!infile.eof())
{
    infile >> firstName[size]
           >> lastName[size]
           >> birthYear[size]
           >> birthMonth[size]
           >> birthDay[size]
           >> sex[size];
}

I think you should increment size at each iteration.
(Anyway testing for EOF is not the correct solution)

Please, let us know if this is an assignment or not.
your functions relying on so many array arguments looks dangerous and error prone.

You may want to consider passing a struct filled with data or implementing a class.
also if your assignment allows you consider using std::array instead of plain arrays.
Topic archived. No new replies allowed.