I am trying to implement some simple logic into my program but I am apparently not understanding what the problem is. This is the first time I have worked with file I/O and it is confusing me.
The exercise calls to display data from a file based on user input so that it displays accordingly:
Justice is ranked 406 in popularity among boys.
Justice is ranked 497 in popularity among girls.
If a name is found in boy names but not girl names then the following should be displayed:
Walter is ranked x among boys.
Walter is not ranked among the top 1000 girl names.
You need to separate your search logic from your result printing.
Your loop should be searching for the names and finding out what they are ranked. You should output the results after the search is complete. It's impossible to print accurate results when you are still in the process or searching.
The reason that your getting 1K lines of output is that your condition for the while loop states "while the input from 'babyFile' is valid and going into 'rank' execute this code...". There are so many ways to go about doing this from basic functions to STL containers, I would suggest the later but do you have a preference?
while (babyFile >> rank)
{
babyFile >> boyName;
babyFile >> girlName;
if (inputName == boyName)
{
cout << inputName << " is ranked "
<< rank << " in popularity among boys. \n";
boysRank = rank;
}
if (inputName == girlName)
{
cout << inputName << " is ranked "
<< rank << " in popularity among girls. \n";
girlsRank = rank;
}
}
if (boysRank < 1 || boysRank > 1000)
{
cout << inputName << " is not ranked in the top 1000 among boys \n";
}
if (girlsRank < 1 || boysRank > 1000)
{
cout << inputName << " is not ranked in the top 1000 among girls \n";
}
I could do all of the logic outside of the while loop but chose to leave it like it is since I am tire4d of pouring over this lol. Thank you for the help.