Yes, this is homework. This is my first course in programming and I am having a problem translating what I know I need to do into code.
I basically need to write a progam that reads a person's name in the following format: first name, then middle name or initial, and then last name. The program then outputs the name in the follow format:
last name, first name middle name.
Seems simple enough... I got that part, but...
The program should allow for users to not give a middle name or initial.
I've attempted this numerous time. I'm figuring that I can create separate functions that either takes in 2 or 3 variables but so far my program does not work when I input first and last name.
This is what I have so far...
I'm probably all over the place, but this class is online and I don't have the luxury of chatting with the instructor. I'm trying to learn pretty much on my own at this time.
Thanks for your assistance in advance.
#include <iostream>
#include <string>
#include <conio>
using namespace std;
class Name
{
public:
Name::Name();
Name::Name(string _first, string _middle, string _last);
Name::Name(string _first, string _last);
Name::Name(istream& ins, Name& the_name);
void Name::input(istream& cin, Name& the_name);
void Name::output(ostream& cout, Name& the_name);
void new_line( );
string fName, mName, lName, record_name;
};
int main( )
{
Name aName;
char a;
aName.input(cin, aName);
aName.new_line( );
aName.output(cout, aName);
cout << "\n";
cout << "enter a character to terminate.";
cin >> a;
getche();
return 0;
}
void Name::new_line( )
{
using namespace std;
char next_char;
do
{
cin.get(next_char);
} while (next_char != '\n');
}
Name::Name(string _first, string _middle, string _last)
{
fName = _first;
mName = _middle;
lName = _last;
}
Name::Name(string _first, string _last)
{
fName = _first;
lName = _last;
}
Name::Name(istream& ins, Name& the_name)
{
cout << "Enter your full name \n";
cout << "(middle name is optional, could be complete or abbreviated):\n";
cout << "\n";
ins >> the_name.fName >> the_name.lName;
}
Name::Name()
{
}
void Name::input(istream& ins, Name& the_name)
{
cout << "Enter your full name \n";
cout << "(middle name is optional, could be complete or abbreviated):\n";
cout << "\n";
ins >> the_name.fName >> the_name.mName >> the_name.lName;
}
void Name::output(ostream& out, Name& the_name)
{
cout << "Your name in our records is: ";
out << the_name.lName + ", " + the_name.fName + " " + the_name.mName;
}