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;
}

Recommended Answers

All 4 Replies

I think the program must know how many words will the user enter...

Try to ask how many words will the user enter and orientate the program to this...

>Try to ask how many words will the user enter and orientate the program to this...
Na, you could just use getline(), which is far more powerful than cin. Since I'm in a good mood today, I've written how it would look:

string line;
    getline(ins, line);
    istringstream convert(line);
    
    // extract first name
    if (! (convert >> line) ) {
        return;
    }
    the_name.fName = line;
    
    // extract middle name
    if (! (convert >> line) ) {
        return;
    }
    the_name.mName = line;
    
    // extract last name
    if (! (convert >> line) ) {
        // if there is no last name, previous must have been middle
        the_name.lName = the_name.mName;
        the_name.mName = "";
    }
    else {
        the_name.lName = line;
    }

The trick here is using stringstreams to parse out the words. If an extraction fails, the stringstream object is nulled, and thus you can check the results of the operation. To use stringstreams however, be sure to include <sstream> .

If the last operation fails, it means that the user hasn't entered a middle name, so instead the "middle" name is transferred over to the last name, and the middle name set to null.

A couple (or maybe 3) other things...

  • Simply use cin.get() to pause your program. You're no longer forced to rely on conio(.h?) .
  • A better way of flushing the input bufer is using cin.ignore:
    cin.ignore(numeric_limits<streamsize>::max());
  • You won't need to flush the input buffer if you use getline.

Thanks joeprogramming. I've had not yet learned of stringstream, but it's always good to learn something new. I'll have to read up on this. Programmig is pretty fun, but these initial steps of learning is painful. Thanks for the sample code, but I won't be turning this in for credit since you wrote it.

Well, if you aren't supposed to use stringstreams, then the only option is the one previously suggested: prompt the user for how many names the person has before asking them to enter them. Which is probably what your instructor wanted anyway.

And I do have to commend you for being honest and not using the code I posted. There aren't very many newbies here who would do the same as you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.