Help with output

I am reading in a file that is last name, first name middle name (if it exists) and trying to output as first name middle name (if it exists) last name. This is what I have so far.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
Name: Jacob L. Andrews
Date: 2/ 5/ 2013
Program 01: Make a program which sorts names that are in the order lastName, firstName middleName to a
different order that is firstName middleName lastName.
*/

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

int main() {

string firstName, middleName, lastName;
string filename;
ifstream fin;
cout << "Enter your input file name: " ;
cin >> filename;
fin.open(filename.c_str());
while (!fin) {
cout << "Please enter a VALID file name:";
cin >> filename;
fin.open(filename.c_str());
}
  string name="";
if (fin) {
    while (getline(fin, name)) {
    name.append(" ");
    int pos1= name.find_first_not_of(" ");
    int pos2= name.find("," + 1);
    int pos3= name.find_last_of(" ");
    int pos4= name.size();
    lastName = name.substr(pos1, pos4 - (pos3+pos2));
    firstName = name.substr(pos2, pos4 - (pos1+pos3));
    middleName = name.substr(pos3, pos4 - (pos1+pos2));
    cout<< firstName<< middleName<< lastName;
}
}

fin.close();
return 0;
}
What is you question?

Some comments:
1) I would interpret the instructions as meaning to write the output to a different file rather than to cout.

2) Your output to cout is going to concantenate the fields with no spaces between the parts of the name.

3) Your find at line 33 is going to look for a null character. You're adding one to the address of a pointer to a one character string. That will point to the null terminator at the end of the string. You probably mean the following:
 
int pos2= name.find(',') + 1;  // return position after the comma 


Oh, sorry for not being more explicit. Basically I am trying to get the output to the right format, and it really isn't coming out as I would expect it to. The instructions were to just to cout the newly arranged strings. I'll make the change you suggested though.
Can you give an example of what the results are, and what it should look like instead?
I forgot about this actually and figured out things myself. My logic was waaay off. I'll close this thread, but thanks for the help.
Topic archived. No new replies allowed.