Help Please Im Getting Frustrated

This is the problem i am supposed to solve : Proceed as in Exercise 6, but design the function to accept a single string object
Consisting of a first name, a middle name or initial, and a last name. As before,
the function should return a single string consisting of the last name, followed by
a comma, and then the first name and middle initial. For example, given "John
Quincy Doe", the function should return the string "Doe,John Q."


Here is my code thus far :

#include <iostream>
#include <string>
using namespace std;

void swap (string , string , string );
string name (string);
int main()
{

string first,middle,last;
string fullname;
cout <<"Enter your fullname [first,middle and last] : ";
cin >> fullname;

cout <<"\nThe name you entered is: "<< fullname ;

swap ( first,middle,last);

cout << "\nYour listed name is: " << first +"," + last + " "+ middle.at(0)<< endl;
}

void swap (string first,string middle,string last)
{
first.swap(last);

}
string str (string fullname)
{

int length = fullname.length();

for (int i=0; i < fullname.length(); i++)
{

if (fullname.at(i) == ' ' )
{

int pos1 = i ;

string first = fullname.substr(i, pos1);

int pos2 = fullname.find (' ' , pos1 + 1 );

string middle = fullname.substr (pos1 +1, pos2 - pos1 -1);

string last = fullname.substr (pos2 +1, fullname.length()- pos2 -1);

}



}
}

The program ask you to enter the name and then it crashes
closed account (ywTRfSEw)
On the output before you have to put in your full name, have you tried changing the just semicolon to <<endl;

Sometimes works for me.
Do you realize that the extraction operator>> stops processing the input when it encounters a white space character?

In main() where do you assign a value to first middle or last?

That last cout in main() should be causing problem, a warning if not an error.

Please use code tags when posting code.
No it dosent work @ thechosengeorge
Topic archived. No new replies allowed.