0

CLICK HERE TO SEE THE IMAGE PROBLEM

C++ - I'm having a big problem with my code and I don't understand where I did the mistake because I'm not getting the result that I want for example the 1st output that problem is asking me :(

My FULL CODE

Outputs that I'm getting wrong

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

int main() {

   string firstName, middleName, lastName, theName;
   
   getline(cin, theName);
   
   int findN = theName.find(" ");
   firstName = theName.substr(0, findN);
   int findN2 = theName.find(" ", findN + 1);
   if (findN2 != string::npos){
      middleName = theName.substr(findN2 + 1, findN2 - findN - 1);
      lastName = theName.substr(findN2 + 1, theName.length() - findN2 - 1);
      cout << lastName << ", " << firstName[0] << "." << middleName[0] << "." << endl;
      }
      else {
         lastName = theName.substr(findN + 1, theName.length() - findN - 1);
         cout << lastName << ", " << firstName[0] << " . " << endl;
         }
   

   return 0;
}
Zahid
  • 1
  • 1
  • 2
  • Any chance you could paste the code as raw in here? – pesuww Oct 03 '20 at 00:24
  • 1
    Okay I did edition to my post and now you can see my full code :D – Zahid Oct 03 '20 at 00:42
  • Please post code, errors, sample data or textual output here as plain-text, not as images that can be hard to read, can’t be copy-pasted to help test code or use in answers, and are barrier to those who depend on screen readers. You can edit your question to add the code in the body of your question. For easy formatting use the `{}` button to mark blocks of code, or indent with four spaces for the same effect. The contents of a **screenshot can’t be searched, run as code, or copied and edited to create a solution.** – tadman Oct 03 '20 at 00:43
  • Wouldn't middle name be `middleName = theName.substr(findN + 1, findN2 - findN - 1);`? – David C. Rankin Oct 03 '20 at 00:48

2 Answers2

1

I suggest learning to use debugger.

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

int main() {

    string firstName, middleName, lastName, theName;

    getline(cin, theName);

    int findN = theName.find(" ");
    firstName = theName.substr(0, findN);
    int findN2 = theName.find(" ", findN + 1);
    if (findN2 != string::npos) {
       //changed from findN2 + 1 to findN + 1
        middleName = theName.substr(findN + 1, findN2 - findN - 1);
        lastName = theName.substr(findN2 + 1, theName.length() - findN2 - 1);
        cout << lastName << ", " << firstName[0] << "." << middleName[0] << "." << endl;
    }
    else {
        lastName = theName.substr(findN + 1, theName.length() - findN - 1);
                           //fixed the white space " . " -> ". "
        cout << lastName << ", " << firstName[0] << ". " << endl;
    }


    return 0;
}
pesuww
  • 105
  • 11
  • Two suggestions. It is always worth mentioning [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721/364696) and understanding the difference [C++: “std::endl” vs “\n”](https://stackoverflow.com/questions/213907/c-stdendl-vs-n). Actually 3 -- you should change `int` to `size_t` as that is the proper type for the return of `.find()` and eliminates the warning of comparison between signed and unsigned types with `findN2 != string::npos`. – David C. Rankin Oct 03 '20 at 00:58
  • Good points. I didn't want to edit the code too much so I just decided to stick with the minimal changes. – pesuww Oct 03 '20 at 01:01
  • Yes, it's always a balance to strike between helping with the exact issue,and pointing out additional improvements. That's why it may be something you just mention or drop in a footnote. What you are trying to overcome is there are some very bad C++ instructors out there that may think comparing `int` and `size_t` is just fine, etc.. Always try to ensure the questioner comes away pointed in the right direction. These comments will suffice for this question. – David C. Rankin Oct 03 '20 at 01:05
0

This is easily solved with debugger.

If there is a second space, both of your middleName and middleName are assigned substring starting from findN2 + 1.

If there is no second space, you have extra spaces around . here: " . "

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27