How to change output given only 2 inputs instead of 3?

I'm in an intro to C++ class and I'm having trouble with one of my assignments.

We need to write a code that can take a person's full name as input and output either lastName, firstName middleInitial or if no middle name is given output lastName, firstName.

I have written the code to do the first, but I'm not sure how to get the program to know if it's given a middle name or not in order to avoid outputting ", firstName lastInital" when there's no middle name. And input is always formatted as firstName middleName lastName.

Below is my code so far. I've been thinking getline would be useful but we haven't learned that function yet.

It's also difficult because the program we use for our labs uses pre-loaded text so if I cin one variable it stops at the first space after a non-space character.

Clarification: Basically, if there were a way to tell how many inputs I was getting, I could write an if-else statement and I could have the correct code under the corresponding branch of the statement. Not sure how to do that though...

My code:

#include <iostream>
#include <string>
using namespace std;

int main() {

string firstName;
string middleName;
string lastName;

cin >> firstName;
cin >> middleName;
cin >> lastName;

cout << lastName << ", ";
cout << firstName << " ";
cout << middleName.at(0) << "." << endl;

return 0;
}

Again, my code runs fine, except when no middle name is given, in which case it outputs incorrectly and I'm not sure how to fix it.. any guidance would be helpful! Sorry if this is long or stupid, but I'm stumped.
Last edited on
So I fixed it, but I'm sure there's a better way than how I did it...

if (lastName != "") {
cout << lastName << ", ";
cout << firstName << " ";
cout << middleName.at(0) << "." << endl;
}
else {
lastName = middleName;
cout << lastName << ", ";
cout << firstName << endl;
}

I made an if-else statement so that if nothing was in lastName then it would reassign what was initially the middleName as lastName and print it out properly. It worked for my assignment, but I'd love to hear and see better ways to do this because this seems like a pretty silly way of doing it. Or maybe it's the best? Please share still!
This (checking if the last name is empty) will work if the input is from a file (end of file will cause the attempted input of the last name to fail).

If the input is interactive (typed in from the keyboard), and the user does not type in something that signals eof, it will wait for the last name to be entered. To handle this gracefully, something more elaborate would be required. For instance, something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <string>
#include <cctype>

int main() {

    std::string first_name ;
    std::string middle_name ;
    std::string last_name ;

    // read in the first two strings
    std::cin >> first_name >> middle_name ;

    // try to read a third string
    char c ;
    // skip bank characters (read and discard till a non-blank character is encountered)
    // http://www.cplusplus.com/reference/istream/istream/get/
    // http://www.cplusplus.com/reference/cctype/isblank/
    while( std::cin.get(c) && c != '\n' && std::isblank(c) ) ;

    if( c == '\n' ) { // if there is no middle name (we encountered a new line)

        last_name = middle_name ; // what we read as the middle name is actually the last name
        middle_name = "" ; // and there is no middle name
    }

    else { // there is a last name, read it in

        // put the first character of the last name back into the input buffer
        std::cin.putback(c) ; // http://www.cplusplus.com/reference/istream/istream/putback/
        std::cin >> last_name ; // and read in the last name
    }


    std::cout << last_name << ", " << first_name ; // print first_name, last_name

    // if there is a middle name, print the middle initial (upper case)
    // http://www.cplusplus.com/reference/cctype/toupper/
    if( !middle_name.empty() ) std::cout << ' ' << char( std::toupper(middle_name[0]) ) << '.' ;

    std::cout << '\n' ; // finally print a new line
}
Thanks!

The program we use for our assignments is via an online textbook that takes input oddly. It's all pre-loaded so it's basically like it's coming from a text file.

I'd be curious to know how many of our assignments would fail if they were in an independent IDE that runs more interactively than our textbook's version of a coding platform.

Luckily my solution worked for this assignment so it's all good though... this forum is making me realize that I'm basically a beginner of a beginner because most of the other questions are still more complex than anything I could understand.

Thanks for your help though!
Topic archived. No new replies allowed.