944,155 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2433
  • C++ RSS
Dec 2nd, 2005
0

need help on C-string

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fidelljf is offline Offline
6 posts
since Dec 2005
Dec 2nd, 2005
0

Re: need help on C-string

Do you know how to input a line of text and parse it?
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 2nd, 2005
0

Re: need help on C-string

no I dont
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fidelljf is offline Offline
6 posts
since Dec 2005
Dec 2nd, 2005
0

Re: need help on C-string

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.
C++ Syntax (Toggle Plain Text)
  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.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Dec 2nd, 2005
0

Re: need help on C-string

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

Re: need help on C-string

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.
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Workaround for declaring a global .net framework object
Next Thread in C++ Forum Timeline: i not understand how i do this problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC