I have to write a program to check the ranking of the top 1000 boy and girl baby names using linked lists and pointers.

I have based most of my work off a tutorial from this website
http://richardbowles.tripod.com/cpp/linklist/linklist.htm

And here is the link to the list of names. You probably have to save it to a file named babynames2004.txt for the program to work.

http://www.cs.nccu.edu/~gmilledge/comp1520/comp1520Spring2007/Homework/babynames2004.txt

My problem is that I am not getting any results. I think it has something to do with writing the data, which consists of a rank, boy name and girl name, to the linked list.

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;


struct name
       { int rank;
         string boy_name;
         string girl_name;
         name *nxt;
         };
         
name *start_ptr = NULL;
name *current;

void add_names();
void get_name(string names);
void check_name();

int ranking;
string enter_name;


int main()
{   get_name(enter_name); 
    add_names();
    check_name();
    system("PAUSE");
    return 0;
}



void add_names()
{   name *temp, *temp2;
    temp = new name;
    int ranking;
    int i;
    string male_name, female_name;
    ifstream in_stream;

    in_stream.open("babynames2004.txt"); //opens and loads names & ranks from file
    if (in_stream.fail())
   {
    cout << "File failed to open." << endl;
    cout << "Program will now close." << endl;
    system("PAUSE");
    exit(0);
   }
   
    in_stream >> ranking >> male_name >> female_name;
    temp->rank = ranking;
    temp->boy_name = male_name;
    temp->girl_name = female_name;
    temp->nxt = NULL;
    
    if (start_ptr == NULL)
    {
    start_ptr = temp;
    current = start_ptr;
    }
    else
        { temp2 = start_ptr;
        while (temp->nxt != NULL)
        { temp2 = temp2->nxt;
        }
        temp2->nxt = temp;
        }
        }
   
void get_name(string names)
{    
     cout << "Please enter a first name to see its ranking" << endl;
     cout << "among the top 1000 baby names." << endl;
     cin >> names;
     cout << endl;
}
void check_name()
{   name *temp;
    temp = start_ptr;
    cout << endl;
    if (temp == NULL)
       cout << "No names found!" << endl;
    else
        { while (temp != NULL)
                if (enter_name == temp->boy_name)
                {
                cout << temp->boy_name << " is ranked " << temp->rank;
                cout << " in popularity among boys.\n" << endl;
                }
                else
                {
                cout << temp->boy_name << " is not ranked among the";
                cout << " top 1000 boy names." << endl;
                }
                if (enter_name == temp->girl_name)
                {
                cout << temp->girl_name << " is ranked " << temp->rank;
                cout << " in popularity among girls." << endl;
                }
                else
                {
                cout << temp->girl_name << " is not ranked among the";
                cout << " top 1000 girl names." << endl;
                }
    temp = temp->nxt;
         }
}

Any help would be much appreciated. THANKS.

I think what is happening is that only the first line of the external file is being added to the program. Any thoughts on how to do the entire file? Maybe some of kind loop?

void add_names()

{ name *temp, *temp2;
temp = new name;

int ranking;
int i;

string male_name, female_name;

ifstream in_stream;

in_stream.open("babynames2004.txt"); //opens and loads names & ranks from file

if (in_stream.fail())
{
cout << "File failed to open." << endl;
cout << "Program will now close." << endl;
system("PAUSE");
exit(0);
}

in_stream >> ranking >> male_name >> female_name;

temp->rank = ranking;
temp->boy_name = male_name;
temp->girl_name = female_name;
temp->nxt = NULL;

if (start_ptr == NULL)
{
start_ptr = temp;
current = start_ptr;
}
else
{ temp2 = start_ptr;
while (temp->nxt != NULL)
{ temp2 = temp2->nxt;
}
temp2->nxt = temp;
}
}

Any help would be much appreciated.

I retooled your method a little bit:

void add_names()
{   //name *temp, *temp2;
    //temp = new name;
    name * prior = new name;// * temp;
    int ranking;
    int i=0;
    string male_name, female_name;
    ifstream in_stream("babynames2004.txt");
    
    //in_stream.open; //opens and loads names & ranks from file
    if (in_stream.fail())
   {
    cout << "File failed to open." << endl;
    cout << "Program will now close." << endl;
    system("PAUSE");
    exit(0);
   }
    
    in_stream >> ranking>>male_name >> female_name;
	
    
    prior = start_ptr;
    prior->rank = ranking;

    prior->boy_name = male_name;
    prior->girl_name = female_name;
    prior->nxt = NULL;
    
    name *temp;
	while(in_stream >> ranking >> male_name >> female_name)
    {
    
    temp = new name;
    prior->nxt = temp;
    
    temp->rank = ranking;
    temp->boy_name = male_name;
    temp->girl_name = female_name;
    temp->nxt = NULL;
    prior = temp;
    
    }
    temp->nxt = NULL;
    
    for (name * strt = start_ptr;strt->nxt !=NULL;strt=strt->nxt)
           cout<<strt->rank<<" "<<strt->boy_name<<endl;
}void add_names()
{   //name *temp, *temp2;
    //temp = new name;
    name * prior = new name;// * temp;
    int ranking;
    int i=0;
    string male_name, female_name;
    ifstream in_stream("babynames2004.txt");
    
    //in_stream.open; //opens and loads names & ranks from file
    if (in_stream.fail())
   {
    cout << "File failed to open." << endl;
    cout << "Program will now close." << endl;
    system("PAUSE");
    exit(0);
   }
    
    in_stream >> ranking>>male_name >> female_name;
	
    
    prior = start_ptr;
    prior->rank = ranking;

    prior->boy_name = male_name;
    prior->girl_name = female_name;
    prior->nxt = NULL;
    
    name *temp;
	while(in_stream >> ranking >> male_name >> female_name)
    {
    
    temp = new name;
    prior->nxt = temp;
    
    temp->rank = ranking;
    temp->boy_name = male_name;
    temp->girl_name = female_name;
    temp->nxt = NULL;
    prior = temp;
    
    }
    temp->nxt = NULL;
    
    for (name * strt = start_ptr;strt->nxt !=NULL;strt=strt->nxt)
           cout<<strt->rank<<" "<<strt->boy_name<<endl;
}

that's after defining start_ptr = new name; in main. I tested it with your other methods and they crashed (saying the last name in the list wasn't in the top 1000 over and over again). I haven't looked into that but this revision takes care of getting the names in -- you did need a loop, but I grabbed the head pointers data outside of it so it could have access to head->next at the proper place. The list does seem to print out in it's entirety.
[aside, there's a stray int i in there and I couldn't remember which of us had put that in]

I would discourage against your use of global variables (except maybe, maybe your head pointer but even that's debatable). int ranking especially is overshadowed by its local counterpart in the add function.

Finally, I would encourage you to put in a deletelist() method (only a couple of lines long) where you march along your list and delete the nodes until you hit the end (NULL). It'll prevent the memory you used in the new calls from leaking.

commented: Saved my life on a project thanks so much +1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.