Can someone help describe this code?

I finished this with help from my professor but still dont understand some things. What is int rank initialized to? Also how does the program know if the name is boy or girl?

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
53
54
55
56
57
58
59
60
61
62
63
64
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>

int main ( )
{
 using namespace std;

 string inputName, 
	 boyName, 
	 girlName;
 ifstream babyFile;
 int rank;
 int boysRank = 0;
 int girlsRank = 0;
 
 
  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";
	   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";
  }


  
 babyFile.close();

 return 0;
}
1) rank isn't initializated. However you are writing into it before you read anything, so everything is fine
2) File have following structure:
<1st boy name> <1st girl name> <2nd boy> <2nd girl>...
So it is easy tot tell if name is boy or girl
Topic archived. No new replies allowed.