Baby names

I have to go through a file with the top 1000 baby names of 2004. The file is formatted like
1 Jacob Emily
2 Michael Emma...
The user needs to be able to enter a name and the program will tell them if it's in the list or not. So if the user enters the name "Walter", then the program should output:

Walter is ranked 366 in popularity among boys.
Walter is not ranked among the top 1000 girl names.

I probably made a stupid mistake but nonetheless I can't figure out why it's messing up. It always says the name I put in is not ranked for boys or girls. Here's the code, if you can help I'll really appreciate it!

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 <fstream>
using namespace std;

const int RANK = 1000;
int n;

int main()
{
	ifstream names;
	string target, boyname, girlname;
	bool findBoy, findGirl;
	int rank, i;
	int rankArray[RANK];
	string boyArray[RANK];
	string girlArray[RANK];
	names.open("babynames2004.txt");
	
while(names >> rank >> boyname >> girlname){
		for(i=0;i<RANK;i++){
		rankArray[i]=rank;
		boyArray[i]=boyname;
		girlArray[i]=girlname;
	}
}
	cout << "Enter a name: ";
	cin >> target;
	cout << endl;
	
for(n=0;n<RANK;n++){
	if(target==boyArray[n]){
		cout << target << " is ranked " << n << " in popularity among boys." << endl;
		findBoy=true;
	}
	if(target==girlArray[n]){
		cout << target << " is ranked " << n << " in popularity among girls." << endl;
		findGirl=true;
	}
}
	if(findBoy!=true) cout << target << " is not ranked among the top 1000 boy names." << endl;
	if(findGirl!=true) cout << target << " is not ranked among the top 1000 girl names." << endl;
	
	return 0;
}
Let's break down your code a little:

1
2
3
4
5
6
7
8
while(names >> rank >> boyname >> girlname) // get each pair of names in turn
{
		for(i=0;i<RANK;i++){ // and write that name into EVERY element of the array
		rankArray[i]=rank;
		boyArray[i]=boyname;
		girlArray[i]=girlname;
	}
}


Each time you read a pair of names, you're filling in 1000 elements with that same name. Then, you read the next name, and fill in 1000 elements with that name, then you read the next name, and fill in 1000 elements with that name, then you read the next name, and fill in 1000 elements with that name, then ... and so on and so until, until the 1000th name, at which point you have the same name in every element of boyArray and the same name in every element of girlArray.

You must learn to debug. There's no trick to it. Something like this would have solved this for you in seconds:

1
2
3
4
5
6
7
8
9
10
while(names >> rank >> boyname >> girlname){
		for(i=0;i<RANK;i++){
		rankArray[i]=rank;
 cout << "Writing value " << rank << " to rankArray[" << i <<"]" << endl;
		boyArray[i]=boyname;
 cout << "Writing value " << boyname << " to boyArray[" << i <<"]" << endl;
		girlArray[i]=girlname;
 cout << "Writing value " << girlname << " to girlArray[" << i <<"]" << endl;
	}
}
Last edited on
Yeah that would have been smart. I was convinced that wasn't even the part that was the problem. But I got it now, thanks!
Topic archived. No new replies allowed.