I'm working on a problem where the user inputs the first name, middle name and last name and should produce the output, last name, first name then midde initial.
example:

input: John Smith Doe

output: Doe, John S.

the program should also work even if the user does not put the middle name.
example:

input: John Doe
output: Doe, John

So far my program takes care only of the first example and I can't figure out on how to take care of the second example.
So far here is what I got.

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;


int main()
{ using namespace std;

	string first_name, last_name;
	char middle_name[20];

	cout << "Enter your first name, middle name or initial and last name in that order\n";
    cin >> first_name >> middle_name >> last_name;
    cout << endl;
	cout << last_name << ", " << first_name << " " << middle_name[0] << "." << endl;
	
	return 0;
}

Recommended Answers

All 5 Replies

Do you know how to input a line of text and parse it?

no I dont

Why a char array for the middle name when you are already using strings? If you can use vectors and stringstreams, the normal C++ way, I'd do something like this.

#include <iostream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;

int main()
{
   string line;
   cout << "Enter your first name, middle name or initial and last name in that order\n";
   if ( getline(cin, line) )
   {
      vector<string> name;
      istringstream iss(line);
      while ( iss >> line )
      {
         name.push_back(line);
      }
      if ( name.size() > 2 )
      {
         cout << name[2] << ", " << name[0] << ' ' << name[1][0] << ".\n";
      }
      else
      {
         cout << name[1] << ", " << name[0] << "\n";
      }
   }
   return 0;
}

/* my output
Enter your first name, middle name or initial and last name in that order
John Quincy Public
Public, John Q.
*/

i could have used strings for the middle name which would be the same thing, i was just trying a couple of different things. i know how to use the vectors but I dont know how to use stringstreams. I got lost when you used: istringstream iss(line); while ( iss >> line ) I guess we are limited into using strings and vectors only. I tried to run your program but it would not work. And one more thing our instructor warned us of using one whole string and using getline. He suggested to use three different strings.

I modified my program so that I used three strings. thanks for your help

#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;


int main()
{ using namespace std;

    string first_name, middle_name, last_name;


    cout << "Enter your first name, middle name or initial and last name in that order\n";
    cin >> first_name >> middle_name >> last_name;
    cout << endl;
    cout << last_name << ", " << first_name << " " << middle_name[0] << "." << endl;

    return 0;
}

That works if the user enters three names. If they only enter two names you will have the last name as the middle name, though.

If you're not familiar with stringstreams as demonstrated by Dave Sinkula you can parse the entire line using other techniques such as strtok() or your own method.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.