4

All i need to do is to parse the input string of your full name, into 3 separate names so I can take the first initials of each and print them. Can anyone point me in the right direction?

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;    
int main()
{
    string [parsed_output] = fullName.split(" ");
    string firstName = parsed_output[0];
    string middleName = parsed_ouput[1];
    string lastName = parsed_output[2];
    string fullName;
    char firstLetter = firstName[0];
    char middleLetter = middleName[0];
    char lastLetter = lastName[0];
    cout << "Enter your first name, middle name or initial, and last name separated by                              spaces: \n"; 
    cin >> fullName;

    cout << "Your initials are: " << firstLetter << middleLetter << lastLetter << '\n';
    cout << "Your name is: " << firstName << " " << middleName << " " << lastName << '\n';
return 0;
}
2
  • Check this: stackoverflow.com/questions/5607589/… - After splitting the name, go ahead and iterate over the sub-strings and for each one print the first character, i.e. if length() > 0 print subString[0]. As shown in the answers below, you can also just take the names separately. Unless processing the complete name at once is an absolute requirement. Commented Nov 4, 2013 at 12:25
  • fullName is not declared, and you want to parse it before putting anything into it? I think you need some basics first. Commented Nov 4, 2013 at 12:41

2 Answers 2

3

You're making things way to complicated. What's the meaning of 'parsed_output[0];' and 'fullName' anyway? You could just do:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;    

int main()
{
  string firstName, middleName, lastName;
  cout << "Enter your first name, middle name or initial, and last name separated by                              spaces: \n"; 
  cin >> firstName >> middleName >> lastName;
  char firstLetter = firstName[0];
  char middleLetter = middleName[0];
  char lastLetter = lastName[0];
  cout << "Your initials are: " << firstLetter << middleLetter << lastLetter << '\n';
  cout << "Your name is: " << firstName << " " << middleName << " " << lastName << '\n';
  return 0;
}

The whole Point of std::cin is, that you get an stream to space separated tokens with the extraction operator '>>'. If you want to extract the first, middle & last names by Hand (like in hasans answer), then you would need the whole string in the first place (a string with all 3 names and whitespaces as limiter). To read in input with containing spaces you would use std::getline(std::cin, fullName) instead of std::cin, because std::cin will extract the Input until the first trailing whitespace is encountered.

Sign up to request clarification or add additional context in comments.

Comments

2

You may want to use e.g. std::getline to get the full name, and std::istringstream for the parsing:

std::string fullName;
std::getline(std::cin, fullName);

std::istringstream iss(fullName);

std::string firstName, middleName, lastName;
iss >> firstName >> middleName >> lastName;

1 Comment

Won't std::cin >> fullName stop at whitespace? You would need to use std::getline to require parsing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.