Hey guys, I am trying to figure out how to write this algorithm for a program we are doing. We have to read in names and phone numbers as strings and then store them in a link list. I cannot figure out how to write the code to add the names as they are read in. Every line either has the word "lastname" or "surname" on each line telling you if that value will be the last name. If it were on the end of each line it wouldn't be an issue. The problem is it can be in the middle, also some names do not have a middle name.
We read in, capitalize, and sort by last name. We are to read in the phone numbers as strings or characters. We have learned, arrays (1 & 2D), link lists, structs, classes (barely, as a struct replacement), not vectors or stringstream
so: FName, MName (if there is one), LName, Phone#
I'm maybe needing a counter to see if there are 4 or 3 inputs per line to know if a middle name is needed or is there some kind of flag that lets me know when I have reached the end of each line?
Official instructions:
You are to write a program that will read in names and phone numbers, rearrange each person’s name, and create a sorted phone directory in a link list format. The list has been collected form different places, placed in a file where the names are not in some standard format. A person’s last name may appear last or it may appear first on the input. A label has been associated with each name to identify where the last name appears. Either “surname” or “lastname” appears just before a person’s last name. The other will be his first name followed by optionally middle initial or name. Capitalize the first letters in each part of the name. Below are examples of inputs. Input file: ‘NamesAndPhoneV2.txt’
Read in the names, and arrange them with the first name first, followed by his middle name or initial if it exist followed by the last name. You must use a link list for your data structure and the list must be a sorted list, sorted on the last name. You must insert a new name into the link list and maintain a sorted order link list. Print out the resulting names and phone directory.
Hint: Read input as ‘strings’ even the phone number. Look for ‘\n’ for the end of line character if reading one char at a time, or optionally if the last string you read had a digit in it (ie looking for a digit might be the better way).
lastname Taylor marie denise 939-1512
james wilis surname thomas 261-8342
Stone Rock lastname Brown 544-2372
surname lea lee high 266-8324
stephens lastname reynolds 696-9231
lastname jam Dirdy toe 555-2391
lastname Russell mack 123-1234
surname Lewis Michelle Tee 828-2148
john mark surname marshall 888-2891
lastname Moto hasey 823-8000
o e lastname vey 177-1423
Twosee or surname knocktosee 823-8321
two B surname orknot 394-8425
Mack lastname Camp 284-9843
Cant never lastname kound 940-8324
surname finitee two N 724-8234
lastname ghigher Eve N 398-1453
Ttow surname jammer 924-5231
Uh nuther lastname won 432-4345
surname Lotts lue Knew 743-4594
knot zo lastname much 824-3943
lastname Macperlly sherrly swirly 243-3453
Ah C lastname moto 849-3324
surname much too 883-2923
lastname done Jjust abot 463-9002
Al Dah surname zame 632-7432
lastname auf Tu Phinish 382-8324
|
Needs to look like:
Marie Denise Taylor 939-1512
James Willis Thomas 261-8342
etc...
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
string temp;
string first;
string middle;
string last;
while(infile>>temp)
{
if(temp=="lastname")
{
infile>>last;
//don't know what the next would be cause sometimes
] //its last, first, middle
//sometimes it is just first, middle, then last
if(temp=="surname")
{
infile>>last;
//same problem here
}
}
|
Thank you for taking the time to read this and be patient with me as I am still very new and learning (struggling...)