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