mercredi 8 décembre 2021

C++ name format [closed]

I am new to C++ and would appreciate some guidance. Before I reveal the problem, let me emphatically state that I am looking for GUIDANCE, not an answer; having the answer given to me will not benefit me any! I know other people have asked similar questions on here, but the answers given included coding to which I have not been exposed yet- I'm not trying just to pass the class, I am taking this course to actually learn C++. Ok, here are the instructions:

Write a program whose input is:

firstName middleName lastName

and whose output is:

lastName, firstInitial.middleInitial.

My code is:

if (middleName.size() == 0 ) {
    cout << lastName << ", " << firstName[0] << "." << endl; }
else {
    cout << lastName << ", " << firstName[0] << "." << middleName[0] << "." << endl; }

I have the closing } and return 0; but didn't copy them. It works as expected when there is a middle name, but if there's not then it outputs this:

, t.d

My thinking was that if the middle name has no size, then it would output the last name and middle initial, but it does not. I tried outputting middleName.size() and keep getting zero, even if there is a middle name.

I appreciate any guidance, and know this is a long post, but I wanted to give as much info as I could.

Aucun commentaire:

Enregistrer un commentaire