User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 401,741 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,246 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Views: 1416 | Replies: 5
Reply
Join Date: Dec 2005
Posts: 6
Reputation: fidelljf is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fidelljf fidelljf is offline Offline
Newbie Poster

Help need help on C-string

  #1  
Dec 1st, 2005
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;
}
Last edited by Dave Sinkula : Dec 1st, 2005 at 11:06 pm. Reason: Added [code][/code] tags.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2004
Posts: 3,471
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: need help on C-string

  #2  
Dec 1st, 2005
Do you know how to input a line of text and parse it?
Reply With Quote  
Join Date: Dec 2005
Posts: 6
Reputation: fidelljf is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fidelljf fidelljf is offline Offline
Newbie Poster

Re: need help on C-string

  #3  
Dec 1st, 2005
no I dont
Reply With Quote  
Join Date: Apr 2004
Posts: 3,471
Reputation: Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light Dave Sinkula is a glorious beacon of light 
Rep Power: 16
Solved Threads: 138
Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: need help on C-string

  #4  
Dec 1st, 2005
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.
*/
Last edited by Dave Sinkula : Dec 1st, 2005 at 11:20 pm. Reason: Changed output to include vector size checking.
Reply With Quote  
Join Date: Dec 2005
Posts: 6
Reputation: fidelljf is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
fidelljf fidelljf is offline Offline
Newbie Poster

Re: need help on C-string

  #5  
Dec 1st, 2005
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;
}
Reply With Quote  
Join Date: Jul 2005
Posts: 1,099
Reputation: Lerner is a jewel in the rough Lerner is a jewel in the rough Lerner is a jewel in the rough Lerner is a jewel in the rough 
Rep Power: 9
Solved Threads: 143
Lerner Lerner is offline Offline
Veteran Poster

Re: need help on C-string

  #6  
Dec 2nd, 2005
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.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:37 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC