need help on C-string

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Dec 2005
Posts: 6
Reputation: fidelljf is an unknown quantity at this point 
Solved Threads: 0
fidelljf fidelljf is offline Offline
Newbie Poster

need help on C-string

 
0
  #1
Dec 2nd, 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.

  1. #include "stdafx.h"
  2. #include <string>
  3. #include <iostream>
  4. using namespace std;
  5.  
  6.  
  7. int main()
  8. { using namespace std;
  9.  
  10. string first_name, last_name;
  11. char middle_name[20];
  12.  
  13. cout << "Enter your first name, middle name or initial and last name in that order\n";
  14. cin >> first_name >> middle_name >> last_name;
  15. cout << endl;
  16. cout << last_name << ", " << first_name << " " << middle_name[0] << "." << endl;
  17.  
  18. return 0;
  19. }
Last edited by Dave Sinkula; Dec 2nd, 2005 at 12:06 am. Reason: Added [code][/code] tags.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,361
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: need help on C-string

 
0
  #2
Dec 2nd, 2005
Do you know how to input a line of text and parse it?
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 6
Reputation: fidelljf is an unknown quantity at this point 
Solved Threads: 0
fidelljf fidelljf is offline Offline
Newbie Poster

Re: need help on C-string

 
0
  #3
Dec 2nd, 2005
no I dont
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,361
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 241
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: need help on C-string

 
0
  #4
Dec 2nd, 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.
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <vector>
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. string line;
  10. cout << "Enter your first name, middle name or initial and last name in that order\n";
  11. if ( getline(cin, line) )
  12. {
  13. vector<string> name;
  14. istringstream iss(line);
  15. while ( iss >> line )
  16. {
  17. name.push_back(line);
  18. }
  19. if ( name.size() > 2 )
  20. {
  21. cout << name[2] << ", " << name[0] << ' ' << name[1][0] << ".\n";
  22. }
  23. else
  24. {
  25. cout << name[1] << ", " << name[0] << "\n";
  26. }
  27. }
  28. return 0;
  29. }
  30.  
  31. /* my output
  32. Enter your first name, middle name or initial and last name in that order
  33. John Quincy Public
  34. Public, John Q.
  35. */
Last edited by Dave Sinkula; Dec 2nd, 2005 at 12:20 am. Reason: Changed output to include vector size checking.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 6
Reputation: fidelljf is an unknown quantity at this point 
Solved Threads: 0
fidelljf fidelljf is offline Offline
Newbie Poster

Re: need help on C-string

 
0
  #5
Dec 2nd, 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 Quick reply to this message  
Join Date: Jul 2005
Posts: 1,678
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 264
Lerner Lerner is offline Offline
Posting Virtuoso

Re: need help on C-string

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC