### 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!

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344`` ``````#include #include 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> target; cout << endl; for(n=0;n
Let's break down your code a little:

 ``12345678`` ``````while(names >> rank >> boyname >> girlname) // get each pair of names in turn { for(i=0;i

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:

 ``12345678910`` ``````while(names >> rank >> boyname >> girlname){ for(i=0;i
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.