Baby Names

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.

this is what I have thus far:

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
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>

int main ( )
{
 using namespace std;

 string inputName, boyName, girlName;
 ifstream babyFile;
 int rank;
 char another = 'y';
 
 while (another =='y')
 {
  babyFile.open("babynames2004.txt");
  // verify that it opened
  if (babyFile.fail( ))
  {
   cout << "Can't open babynames2004.txt\n";
   exit(1);
  }

  cout << "Enter the name to search for: ";
  cin >> inputName;

  while (babyFile >> rank)
  {
   babyFile >> boyName;
   babyFile >> girlName;

   if (inputName == boyName)
   {
	   cout << inputName << " is ranked " 
	    << rank << " in popularity among boys. \n";
   }

   if (inputName == girlName)
   {
	   cout << inputName << " is ranked " 
	    << rank << " in popularity among girls. \n";
   }   

  }
  babyFile.close();
  babyFile.clear();
  cout << "Try another name? ";
  cin >> another;
 }
 return 0;
}


I tried implementing my logic as follows but it output the cout 1000 times instead of checking the logic first:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
   if (inputName == boyName)
   {
	   cout << inputName << " is ranked " 
	    << rank << " in popularity among boys. \n";
   }

   if (inputName == boyName && inputName != girlName)
   {
	   cout << inputName << " is ranked " 
	    << rank << " in popularity among boys. \n";
           cout << inputName << " is ranked " 
	    << rank << " in popularity among girls. \n";
   }else
           cout << inputName << " is not ranked in the top 1000 girls \n"; 

   if (inputName == girlName)
   {
	   cout << inputName << " is ranked " 
	    << rank << " in popularity among girls. \n";
   }   


Thanks for any help!

Warmaster
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?
@Computergeek01 Since I am more familiar with functions that would be my preference.

@Disch hmm, can you provide a basic example of what you are referring to with this suggestion?

Thank you.
It's kind of hard without giving the whole thing away.

But basically you want something like this:

1
2
3
4
5
6
7
8
while(...)
{
  // find the girl and boy ranks here
  // do not print anything!
}

// examine girl and boy ranks here
//  print results accordingly 
Ok, I get it now and got it working with the following implementation:

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
  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.

Warmaster
Topic archived. No new replies allowed.