Easy name printing program

Write a C++ program that takes a string containing a full name, and outputs each part of the name separately. The name should be in the form of first, middle, and last name separated from each other by a single space. For example, if the name string contains

“John Jacob Schmidt”

then the program would output:

First name: John
Middle name: Jacob
Last name: Schmidt

I don't even know where to get started this is all I have.

1
2
3
4
5
6
#include <iostream>
#include <string>

using namespace std;

int main() {
closed account (Dy7SLyTq)
just have an array of three strings and then read in each name from the buffer
I'm not sure how to do any of that... first week of programming ever.
closed account (jwkNwA7f)
For arrays: http://www.cplusplus.com/doc/tutorial/arrays/
Or you could have a loop that goes through the string and searches for the blank space in between and separates it from there.

There is also functions in C++ that will allow you to split the string into multiple pieces given certain characteristics. Just Google "C++ Split String".
closed account (Dy7SLyTq)
Or you could have a loop that goes through the string and searches for the blank space in between and separates it from there.


why would you do that and not just:
cin>> FirstName >> MiddleName >> LastName
which will do the same thing
@DTSCode

What the OP stated they needed, was..
Write a C++ program that takes a string containing a full name, and outputs each part of the name separately.

It does state, "A string containing a full name... ", not a bunch of them, or 3. So andrey 117, is correct in their statement.
Last edited on
closed account (Dy7SLyTq)
no hes not because what the op also stated was he wants this for input:
John Jacob Schmidt
and the output to be this:
First name: John
Middle name: Jacob
Last name: Schmidt
cin>> FirstName >> MiddleName >> LastName would do that. of course the names would be different if he chooses, but thats all he needs. cin grabs from the buffer until it reads a space. what do you think getline is for?
Topic archived. No new replies allowed.