isolating substrings

I'm trying to isolate a first name, space, middle initial, space, and last name. The full name is given by the user. The only problem I'm having is isolating the middle initial. Current code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string forwardname,first,mi,last;
int space1pos,space2pos;
cout<<"Enter your first name, middle initial, and last name, separated by";
cout<<" a space: "<<endl;
getline(cin,forwardname);
space1pos=forwardname.find(' ');
space2pos=forwardname.find(' ',space1pos+1);
first=forwardname.substr(0,space1pos);
mi=forwardname.substr(space1pos+1,space2pos);
last=forwardname.substr(space2pos+1);
cout<<first<<endl;
cout<<mi<<endl;
cout<<last<<endl;
system("PAUSE");
return EXIT_SUCCESS;}


Here is the console output:

Enter your first name, middle initial, and last name, separated by a space:
Thomas G Hooper
Thomas
G Hooper
Hooper
Press any key to continue . . .

I've tried a number of different things, but I just can't figure out why mi is outputting the middle initial, second space, AND last name.
Topic archived. No new replies allowed.