/* I am supposed to use the at and length methods. The program needs to accept the a string containing a person's full name in the format of first name space last name. My function must return the first name in one variable and the last name in a separate variable.
I can't figure out how to approach this. Im not sure how the length method is supposed to work. I tried an unrelated sample program and it read the space as well as the letters. My program stops after the first name.*/
#include <iostream>
#include <sstream>
usingnamespace std;
void process( const string & name, string & first, string & last )
{
istringstream sin( name );
sin >> first;
sin >> last;
}
int main()
{
string name, first, last;
// read in a line of input
getline( cin, name );
// split into first and last names
process( name, first, last );
// dump names
cout << first << endl;
cout << last << endl;
return 0;
}
I agree it is kind of silly but thats the way the prof wants it.
I'm trying to read the name, one character at a time and then stop
at the space between the first and last name. store that name in
a variable, then read the last name store that in a separate variable
then use the plus operator to combine the two.
This is what I have so far but it's not compiling.
I appreciate the help.
string sepname(string name)
{
string firstname, lastname;
for(int i=0; i < name.at(""); i++)
{
name = name+i;
}
firstname=name;
for(int j=name.at(""); j<name.length()+; j++)
{
name = name+j;
}
lastname=name;
name = firstname+lastname;
return name;
So I guess the find method is out, too... Well, here's some help:
1 2 3 4 5 6 7 8
// loop that executes once for each character in a string
for( int i = 0; i < name.length(); ++i )
{
if( name.at(i) == ' ' )
{
// the character at position i in name is a space
}
}