954,504 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

need help on C-string

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;
}
fidelljf
Newbie Poster
6 posts since Dec 2005
Reputation Points: 10
Solved Threads: 0
 

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

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

no I dont

fidelljf
Newbie Poster
6 posts since Dec 2005
Reputation Points: 10
Solved Threads: 0
 

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.
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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
#include
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;
}

fidelljf
Newbie Poster
6 posts since Dec 2005
Reputation Points: 10
Solved Threads: 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.

Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You