0

I have to intput first, middle and last name of a person and the output should be last name, first initial and middle initial if the name has one, but I am having problems trying to take out the middle initial and when I don put a middle name the code prints last name, first initial and last initial any solutions?

 #include <iostream>
    #include <string>
    using namespace std;
    
    int main() {
    
       /* Type your code here. */
       string firstName, middleName, lastName, fullName;
       
       getline(cin, fullName);
       
       char firstSpace = fullName.find_first_of(' ');
       char secondSpace = fullName.find_last_of(' ');
       firstName = fullName.substr(0, firstSpace);
       middleName = fullName.substr(fullName.find(' ') + 1 , fullName.find (' ') );
    
       if (secondSpace < 0) {
         lastName = fullName.substr(firstSpace);
         cout << lastName + ", " + firstName.substr(0,1)+ "." << endl;
         
       }
       else{
         lastName = fullName.substr(secondSpace + 1);
         cout << lastName + ", " + firstName.substr(0,1) + "." + middleName.substr(0,1) + "." << endl;
       }
       return 0;
    }
alex perez
  • 17
  • 1
  • 7
  • What's the format of your input line when middle name is present and when is not present? – Vivek Maran Oct 01 '20 at 01:53
  • This seems overly complicated. Why not put `fullName` into a `std::istringstream` and read strings out until input fails? Then you'll know how many names there are and take appropriate action. Or use a `std::regex` to match the different supported name styles and even extract the middle initial automagically. – paddy Oct 01 '20 at 01:56

1 Answers1

0

You can try using firstSpace == secondSpace for the if statment.

str.find_fisrt_of and str.find_last_of returns the index of them. So if there is only 1 space in between, it will return true and enter that logic. For the same reason, firstSpace and secondSpace should probably be declared int instead of char.

Also you are getting your middle name wrong. str.substr takes in two numbers, the first one is the starting index of it, and the second one is the length of it. So instead, you should be doing

middleName = fullName.substr(firstSpace + 1 , secondSpace - firstSpace - 1);

Also you could get the first letter of the string by using str.front(), so you are not creating a new string.

Rather than using + to connect different strings, you should use <<. Using +, you are actually adding all of them in to a temporary string object. Using <<, you are just pushing them into output.

And the majority of your if else will be the same, instead you could have this part written outside of it:

lastName = fullName.substr(secondSpace + 1);
cout << lastName << ", " << firstName.front() << ".";

Then:

if(firstSpace == secondSpace)
    cout << middleName.front() << ".";
cout << endl;

Also, people will mention why using namespace std is a bad practice

So my final code:

#include <iostream>
#include <string>
    
int main() {
    
  std::string firstName, middleName, lastName, fullName;
  std::getline(std::cin, fullName);
       
  int firstSpace = fullName.find_first_of(' ');
  int secondSpace = fullName.find_last_of(' ');

  firstName = fullName.substr(0, firstSpace);
  middleName = fullName.substr(firstSpace + 1 , secondSpace - firstSpace - 1);
  lastName = fullName.substr(secondSpace + 1);

  std::cout << lastName << ", " << firstName.front() << ".";
  if(firstSpace != secondSpace)
  {
    std::cout << middleName.front() << "."; 
  } 
  std::cout << std::endl;
  return 0;
}
Ranoiaetep
  • 5,872
  • 1
  • 14
  • 39