Need help with single string variable + substring getting

I'm trying to write a C++ program that does:

mame input in single string variable first space middle space last

Locate first name + store it in a separate string variable. Display the first name substring along with its length.

Locate middle initial + store it in a separate string variable.

Locate last name + store separate string variable. Display the last name substring with its length.

Form a new string with the last name, a comma, a blank, first name, a blank, and the middle initial. Display the new name.

Display the length of the new combined string formed in the previous step.

Find and display the position of the comma in the formed string.

Swap the first name and last name strings, and display these swapped names.

Heres my code

#include <string>
#include <iostream>

using namespace std;

int main()
{
char Request[] = "Please enter a first name, middle initial, and last name in that exact spacing and format: ";

string First_Name;
string Middle_Initial;
string Last_Name;

cout << Request << endl;

cin >> First_Name >> Middle_Initial >> Last_Name;

int firstNameL = First_Name.length()+3;
int middleInitialL = Middle_Initial.length();
int lastNameL = Last_Name.length();

int totalNameL = firstNameL + middleInitialL + lastNameL;
int commaPosition = Last_Name.length();

after this I just Cout << everything as I need to, and return 0. The program works just fine, but it is not using a single variable string.

I know how to do this problem using individual string variables for each first middle last name, but...

How do I do this using a single string variable and then determine substrings and lengths?

I need the program to store the input into a single variable string, find the substring lengths and save them to individual string variables, then combine them in a different order with a comma into a new single variable string. Then, need to display the position of the comma in the new string, and finally swap first name and last name and display them.




bump
I need the program to store the input into a single variable string


Seeing as they are strings, you can just do the following to get them in to a single string:
string fullName = First_Name + ' ' + Middle_Initial + ' ' + Last_Name;


find the substring lengths and save them to individual string variables

You've already done this:
1
2
3
int firstNameL = First_Name.length()+3;
int middleInitialL = Middle_Initial.length();
int lastNameL = Last_Name.length();



then combine them in a different order with a comma into a new single variable string
:
Use this:
string fullName = First_Name + ' ' + Middle_Initial + ' ' + Last_Name;

But change the order of the variable and change the ' ' for ','


Not quite sure what you mean by this. Swap the data in variables, or change the way they are displayed??
and finally swap first name and last name and display them.


either change the order in the way you 'cout' them or...:

1
2
3
string temp = First_Name;
First_Name = Last_Name;
Last_Name = temp;
well firstNameL is the variable holding the integer which is the length of the name. It isnt a substring holding another string.

If I do cin >> fullName;

then the outputs are all 0
cin >> fullName; //only reads to the first whitespace character (' ', '\t', or '\n')

You probably want:
getline(cin, fullName); //store a full line (including ' ' and '\t' characters) to fullName
Topic archived. No new replies allowed.