First Names Project

Hi everyone. I need help on my homework assignment.
Here are the requirements
Create an application that searches a file of male and female first names. A link to the file is provided on the class webpage. "FirstNames2015.txt" is a list of the most popular baby names in the United States and was provided by the Social Security Administration. Each line in the file contains a boy's name and a girl's name. The file is space-delimited, meaning that the space character is used to separate the boy name from the girl name. The file begins with:

Noah Emma
Liam Olivia
Mason Sophia
Jacob Ava
William Isabella
Ethan Mia
James Abigail

Write a program that asks the user to enter a name. The program then searches the file for the name and displays the position of the name in the file if it matches either a boy name or a girl name. The program also displays a message if the name could not be found. Sample outputs:

Enter a name to search: Daniel
Daniel is ranked 12 for boy names
Daniel was not found for girls

[code]
// THE PROBLEM WITH MY PROGRAM IS THAT EVERYTIME I RUN IT AND ENTER A NAME THAT IS ON THE LIST, IT SAYS THAT IT IS NOT RANKED FOR BOY OR GIRLS NAME

#include "stdafx.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
{
string inputName, boyName, girlName;
ifstream babyFile;
int rank;
int boysRank = 0;
int girlsRank = 0;
char another = 'y';

while (another == 'y')
{
babyFile.open("FirstNames.txt");
// verify that it opened
if (babyFile.fail())
{
cout << "Can't open babyFile.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();
babyFile.clear();
cout << "Try another name? ";
cin >> another;
}

system("PAUSE");
return 0;
}

Last edited on
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
#include "stdafx.h"
#include <string>
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;

int main()
{
string inputName, boyName, girlName;
ifstream babyFile;
int rank=0;
int boysRank = 0;
int girlsRank = 0;
char another = 'y';

while (another == 'y')
{
babyFile.open("FirstNames.txt");
// verify that it opened
if (babyFile.fail())
	{
	cout << "Can't open babyFile.txt\n";
	exit(1);
	}

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

while (babyFile.good())
//while (babyFile >> rank)
	{
	babyFile >> boyName;
	babyFile >> girlName;
	rank++;
	
	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();
babyFile.clear();
cout << "Try another name? ";
cin >> another;
}

// system("PAUSE");
return 0;
}
@SamuelAdams Thank you so much ! It worked for me. Now I can see where my code was incorrect.
Topic archived. No new replies allowed.