0

Hey guys so I have an assignment for class where I have to split a string and manipulate it. However, when I try to split the string and assign it to an array only the first element comes and the other two don't. Please help.

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

using namespace std;

int main()
{
    string str;
    cout<<"Enter a first name, middle initial, last name: ";
    cin>> str;
    string word;
    string array[3];
    stringstream stream(str);
    int counter = 0;
    while( getline(stream, word, ' ') )
    {
        cout<<word;
       array[counter] = word;

       counter++;
    }
    cout<<"The "<<array[0].length()<<" characters of the first name are: "<<array[0]<<endl;
    cout<<"The "<<array[2].length()<<" characters of the last name are: "<<array[2]<<endl;
    string newstring = array[2]+", "+array[0]+" "+array[1];
    cout<<"In the phone book, the name would be: "<<newstring<<endl;
    cout<<"The length of the name is: "<<newstring.length()<<endl;
    cout<<"The comma is at position: "<<newstring.find(",")<<endl;
    array[0].swap(array[2]);
    cout<<"After the swap, the last name is "<<array[2]<<" and the first name is "<<array[0];

    system("pause");
    return 0;

}
4

1 に答える 1