•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 375,174 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 2,236 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.
Please support our C++ advertiser:
Views: 1381 | Replies: 5
![]() |
•
•
Join Date: Dec 2005
Posts: 6
Reputation:
Rep Power: 0
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.
#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.
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.
•
•
Join Date: Dec 2005
Posts: 6
Reputation:
Rep Power: 0
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;
}
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C++ Marketplace
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



Linear Mode