Help with Storing Names in Array


I have a text file that has ranked baby names, 1000 of them. This is supposed to go through the text file and then see if the input matches the name, and then gives the rank of the name.

1) can or should my Array store all the names? and then search through that?

2) down at the first while loop, where I commented, I need to store those names, but I'm not really understanding how to do it.



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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
#include <iomanip>
// debug //
#include <iterator>
#include <algorithm>
//#include <sstream>

const int RANK = 1000;
int z;
using namespace std;


int main()
{

	

	ifstream name; 
	name.open("E:\\homework\\babynames2012.txt");
	string userName, boyName, girlName;
	int rankArray[RANK];
	int rankBoy = 0, rankGirl = 0, rankNum = 0, x; 
	string sucker;
	string input = ""; // user input to begin search for baby name.//
	string bArray[RANK], gArray[RANK]; // array for boys' & girls' names.//
	bool fboy; // search for boys name.//
	bool fgirl; // search for girls name.//

	// debug check file //
	/* 
	if (name.is_open())
	{
		cout << "file is open. \n";
		istream_iterator< char > begin( name ) , end;
		ostream_iterator< char > output( cout );
		copy( begin , end , output );

	}
	else
		cout << "file is NOT OPEN, please Exit Program.\n" ;
		exit(1);
	*/


	
	

	cout.setf(ios::showpoint);
	cout.setf(ios::fixed);
	
	cout << '*' << setw(7) << "Welcome to Gotdayums Baby Name Registry!" << '*'
			<< endl << endl << "Here you can search our database for the 1000 most popular baby names.\n" 
			<< "The most popular name is ranked #1, \n" << "whereas the least popular name in our list " 
			<< "is ranked #1000.\n\n" ;

	cout << "Please enter you credit card number at the prompt:\n>" ;
		
		getline(cin, sucker);

		if (sucker.length() > 1)
			cout << "Thanks sucker.\n" ;
		else 
			cout << "Well played...\n";
		
		do{  

	cout << "Please enter a first name for either a girl or boy: \n>";
	getline(cin, input);
	//lets check if the name was correctly entered.
	if (input.empty())
	{
		cout << "What are you doing fool?.\n" ;
	}
	else
 
	cout << "You entered: " << input << endl << endl;

		}while (input.empty());

	cout << "Please wait while we search our database..\n" ;

/// LOOK HERE /// AT THE WHILE LOOP /// HELP!!

	while(name >> rankNum >> boyName >> girlName)
	{
		
		for (x=0; x < 1; x++) 
	
			rankArray[x] = rankNum;
			bArray[x] = boyName;
			gArray[x] = girlName;
		
	}
	
	for(z=0; z<RANK; z++)
	{
	if(input==bArray[z])
	{
		cout << input << " is ranked " << z << " in popularity among boys." << endl;
		fboy=true;
	}
	if(input==gArray[z]){
		cout << input << " is ranked " << z << " in popularity among girls." << endl;
		fgirl=true;
	}
	}

if(fboy!=true) cout << input << " is not ranked among the top 1000 boy names." << endl;
	if(fgirl!=true) cout << input << " is not ranked among the top 1000 girl names." << endl;

	cin.get();
	//return 0;
}

	

Last edited on
See if this something like this works out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
struct Entry
{
	std::string _boys;
	std::string _girls;
	int popularity;
	std::istream& operator>>(std::istream& is, Entry e)
	{
		is >> popularity >> _boys >> _girls;
		return is;
	}
};

// ....
	
std::ifstream ifs("baby_names.txt");
if(!ifs) std::cout << "ERROR!\n\n";
std::vector<Entry> array;
while(ifs)
{
	Entry temp;
	ifs >> temp;
	array.push_back(temp);
}
1) Yes.
2) Think a minute about what your loops are doing:

1
2
3
for each line in the file:
  for each array item:
    set the item to the data from the line

I think you would rather have something like:

1
2
for each line in the file AND each corresponding array item:
  set the item to to the data from the file

The simplest way to do this is to track the number of items.

BTW, you need to be more careful about how you name things. 'name', for example, is not a very descriptive (or useful) name for a file. You could call it 'nameFile' or even just 'file', for example, which gives you (and whoever else reads the code) a better idea of what the thing is. A file is not a name.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
constexpr int MaxNumItems = 1000;
int    rankNums[MaxNumItems];
string boysNames[MaxNumItems];
string girlsNames[MaxNumItems];
int NumItems = 0;  // <-- the number of names and corresponding ranks actually stored in the arrays

...

ifstream nameFile( ... );
int rankNum;
string boyName, girlName;

while (nameFile >> rankNum >> boyName >> girlName)
{
    rankNums[NumItems] = rankNum;
    boysNames[NumItems] = boyName;
    girlsNames[NumItems] = girlName;

    NumItems++;  // there is now one more item in the array

    // Make sure that you cannot read too many names
    if (NumItems == MaxNumItems) break;
}

I have a couple of suggestions.

1) Keep related information together.

1
2
3
4
5
6
7
8
9
10
11
12
struct RankedNames
{
  int rank;
  string boyName;
  string girlName;

  RankedNames( int rank = 0, const string& boyName = "", const string& girlName = "" ):
    rank(rank),
    boyName(boyName),
    girlName(girlName)
    { }
};

2) Use a vector:

1
2
3
4
5
6
7
8
vector <RankedNames> rankedNames;

...

while (nameFile >> rankNum >> boyName >> girlName)
{
    rankedNames.push_back( RankedNames( rankNum, boyName, girlName ) );
}

When you are done getting the data from the file, you can sort the names by rank:

1
2
3
4
5
6
7
sort( rankedNames.begin(), rankedNames.end() []( const RankedNames& a, const RankedNames& b )
{
    a.rank < b.rank;
} );

cout << "The number one boy's name is " << rankedNames[0].boyName << "and\n";
cout << "the number one girl's name is " << rankedNames[0].girlName << ".\n";

Hope this helps.

[edit] Too slow... I guess this is what happens when you pause to help your niece do her math homework, LOL.

[edit 2] Fixed an emplace_back()-ism.
Last edited on
Thank you very much Megatron 0 and Duoas.

I appreciate the multiple examples, Duoas, everything you wrote is very helpful.
Topic archived. No new replies allowed.