How do I capture the string after a space in a single line of cin?

Write your question here.
I am writing a code that takes a user's full name, swaps the user's last name with another name to be entered by the user and returns the user's modified full name to the console.
My code works entirely except it only captures one word of the last name. According to my assignment the last name could consist of any number of words with spaces in between. I know how to do a search for char, but I don't know how to do a search for space. Could someone please give me an example of a search for space?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  // Joshua Cisneros
// Assignment 4.3
// Due Date: 2/28/16
// Purpose: Write a function with the signature of string swapLastName(const string full_name, const string new_last_name)
// that function should take in the user's full name (first & last), 
// swaps the user's last name with another last name to be 
// entered by the user, and returns the user's modified full name to the console.
#include <iostream>

using namespace std;

int main()
{
    string full_name[2];
    cout << "Enter full name: ";
    cin>>full_name[0]>>full_name[1];
    string change;
    cout << "Enter new last name: ";
    cin>>change;
    full_name[1] = change;
    cout<<full_name[0]<<" "<<full_name[1] << endl;
   
   return 0;
}
Operator>> is used for formatted input. It treats whitespace as delimiter.
See http://www.cplusplus.com/reference/string/string/operator%3E%3E/

The std::getline does unformatted input.
See http://www.cplusplus.com/reference/string/string/getline/
Write a function with the signature ...

I see no such function.

According to my assignment the last name could consist of any number of words with spaces in between

Use getline to capture multiple space separated words. Your challenge will be to isolate the first name from the string returned by getline.

I know how to do a search for char, but I don't know how to do a search for space.

Space is a character.
Last edited on
Well the function I was supposed to use looks like this: string swapLastName(const string full_name, const string new_last_name)

But I gave up on using the professor's function. I just want the project to work so I can get a passing grade. And if my challenge is to isolate the first name from the string how would I do that?
Topic archived. No new replies allowed.