Name Arrange

You are to write a program that will read in upto 15 names, rearrange each person's name. The list has been collected from different places and the names are not in some standard format. A person's last name may appear last or it may appeaer first. 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 the first name followed by optionally middle initial or name. Capitalize the first letters. Examples of inputs are given below.

Read in the names, and arrange them with the last name first, followed by a comma, followed by the first name and his middle name or initial if it exist. Sort the list of names and print out the results.

input file: namesLast.txt

Example input:

tom archer lastname jones

lastname luewey Huewey dewy

See More surname Or-less

Example Output(Sorted):

Jones, Tom Archer

Luewey, Huewey Dewy

Or-less, See More

I really just don't know what I am doing. I somewhat understand the syntax, but I've got a long way to go till I truly understand how and what to do to properly write a code.

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
  #include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    ifstream Name;
    Name.open("NameArrange.in");
    if(!Name)
    {
        cout<< "Error opening NameArrange.in"<<endl;
        return 0;
    }

    string names(15);
    int cnt=0;

    else
    {


    while(
    {
        string LName;
        string FName;
    }

    return 0;
}
string(15) is not 15 strings.
you want
vector <string> names(15);

there are a couple of ways to approach this.
one way is to getline the whole line in the file and bust it up yourself.
another way is to read it a string at a time, and process it that way.

lets look at the second way, its a little easier for a new programmer
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
vector <string> fnames(15); //it would be nicer if these were in a struct together but for now..
vector <string> lnames(15); //we will do 'parallel arrays' where array1[index] is coupled to array2[index]  tied together by index

string tmp = "";

do
{
 file >> tmp; //read a 'word' (delimited by spaces) 
  if(tmp != "lastname")
 {
 fname[index] += tmp;
 fname[index] += " "; //the space was eaten by the reader, put it back in!
}
} while (tmp != "lastname");
file >> lname[index];
...
and later, a print it loop might have
cout << lname[index] << ", " << fname[index] << endl;


this isnt 'done', its a guide and a starting point. These are the ideas you want. Can you do it now? The loop logic and processing are inefficient and direct, easy to read so you can see what I was doing.
Last edited on
I came up with something so much more complicated than that it's ridiculous.
jonnin, I can't use vectors yet. My professor will count off major points using somehting he hasn't covered yet. I once used && and he marked me 5 points.
Getline i can use, just unsure how to properly use it. My professor goes over random things within C++ and i don't have a fundamental knowledge of how things work within C++.

I'm very simple, so if my words have you raising a brow, bare with me.
Can you use arrays? It's almost the same syntax, just replace
vector <string> fnames(15); with string fnames[15];
Topic archived. No new replies allowed.