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!
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;
}
}