| | |
need help on C-string
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2005
Posts: 6
Reputation:
Solved Threads: 0
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.
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)
#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 2nd, 2005 at 12:06 am. Reason: Added [code][/code] tags.
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
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)
#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 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
•
•
Join Date: Dec 2005
Posts: 6
Reputation:
Solved Threads: 0
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;
}
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;
}
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Workaround for declaring a global .net framework object
- Next Thread: i not understand how i do this problem
| Thread Tools | Search this Thread |
api array arrays based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion convert count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






