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