Hey guys,
I have to write a program to check the ranking of the top 1000 boy and girl baby names using linked lists and pointers.
The POINT of the PROGRAM is if you enter a name it will tell you what the name ranks among the list if it is ranked at all.
Result cout should look like this.
Jacob is ranked #1 in popularity among boys.
Jacob is not ranked in the top 1000 girl names.
My problem is in my void check_name() function... the function that checks if the name is in the list and then displays it as well. When the program runs for some reason it only works for the first set of names (the #1 boy and girl) but in an infinite loop.
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.
Sorry if this is confusing Ive been working on this program for way too long.
#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 check_name(); string enter_name; int main() { start_ptr = new name; cout << "Please enter a first name to see its ranking" << endl; cout << "among the top 1000 baby names." << endl; cin >> enter_name; cout << endl; add_names(); system("PAUSE"); return 0; } 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; } void check_name() { name *strt; strt = start_ptr; cout << endl; for (name * strt = start_ptr;strt->nxt !=NULL;strt=strt->nxt) { if (enter_name == strt->boy_name && enter_name != strt->girl_name) { cout << enter_name << " is ranked #" << strt->rank; cout << " in popularity among boys.\n" << endl; } else (enter_name != strt->boy_name && enter_name != strt->girl_name) { cout << enter_name << " is not ranked among the"; cout << " top 1000 boy names." << endl; } if (enter_name == strt->girl_name && enter_name != strt->boy_name) { cout << enter_name << " is ranked #" << strt->rank; cout << " in popularity among boys.\n" << endl; } else (enter_name != strt->girl_name && enter_name != strt->boy_name) { cout << enter_name << " is not ranked among the"; cout << " top 1000 girl names." << endl << endl; } } }
Edited by mcap61: n/a