Need help with string.substr()

Hi All. I am needing some assistance setting up this program. I would like it to show the 1st initial of each the first, middle and last name. I would also like it to print the first 3 letters of the person's middle name on the fifth line, print the eighth character in the persons full name on the sixth line, and print the total number of characters in the person's name (including the spaces) on the seventh line.

Here would be the ideal outcome:
Enter a name in the format First Middle Last: James Tiberius Kirk
James
Tiberius
Kirk
JTK
Tib
i
19

Here is what I have so far:
#include <iostream>
#include <string>
using namespace std;
char chr;
int main()
{
string first, middle, last;
cout << "Enter your first name, middle name, and last name" << endl;
cin >> first name >> middle name >> last name;
cout << string.substr

}
You could do something like
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

int main()
{
     std::string first, middle, last;
     std::cout << "Enter your first name, middle name, and last name" << std::endl;
     std::cin >> first >> middle >> last;
     //Output each part of the name
     //Output the first character of each part of the name (hint use the .at() function)
     //Use middle.substr()
     //Turn them all into one single string using += (don't forget to add the spaces)
     //Output the eighth character using .at()
     //Count the length of the name
     return 0;
}

http://www.cplusplus.com/reference/string/string/at/
http://www.cplusplus.com/reference/string/string/substr/

P. S. Check your input, it won't work the way you have it currently.
P. P. S. Use the code tags please.
@Too Explosive,

Turn them all into one single string using +=

I tried that first += ' ' + middle + ' ' + last, but that gave me a run time errorUnhandled exception at 0x7647DAE8 in Sub String.exe: Microsoft C++ exception: std::out_of_range at memory location 0x00AFF458. Using Visual Studio 2015. Solved the problem with temp = first + ' '.... Of course it was late and I did not want to figure it out right then.

Do not know if it will make any difference or if it was just something I did wrong.

Andy
If it's throwing that I believe you have to just add them one by one.
1
2
3
4
first += ' ';
first += middle;
first += ' ';
first += last;
I'm not a hundred percent sure on why it is throwing that though.
Last edited on
@gracehoward628,
I was tempted to post the full solution but I am sure if it would help you.
What kind of assistance do you want / need ?
Topic archived. No new replies allowed.