Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

With relation to the code I have given below can you guys help me answer these q

ID: 3859159 • Letter: W

Question

With relation to the code I have given below can you guys help me answer these questions?

#include <iostream>

#include <string>

#include <typeinfo>

using namespace std;

int main()

{

string name, rname;

string fname, mname, lname;

fname = mname = lname = "";

cout << " Welcome to the name Formalizing program!!!";

cout << " Enter your name: ";

getline(cin, name);

fname = name.substr(0, name.find(' '));

if (fname.length()>0){

name = name.substr(fname.length()+1);

mname = name.substr(0, name.find(' '));

}

if (mname.length()>0){

if(mname.length() == name.length())

lname = "";

else

lname = name.substr(mname.length()+1);

}

if (lname.length()>0)

{

//Make all charecters lower first

for(unsigned short i=0;i < lname.size();i++)

{

lname[i]=tolower(lname[i]);

}

  

for(unsigned short i=0;i < fname.size();i++)

{

fname[i]=tolower(fname[i]);

}

  

for(unsigned short i=0;i < mname.size();i++)

{

mname[i]=tolower(mname[i]);

}

//make first charecter upper

lname[0] = toupper(lname[0]);

fname[0] = toupper(fname[0]);

mname[0] = toupper(mname[0]);

rname = lname + ", " + fname + " "+ mname[0] + ".";

}

else

{

for(unsigned short i=0;i < fname.size();i++)

{

fname[i]=tolower(fname[i]);

}

  

for(unsigned short i=0;i < mname.size();i++)

{

mname[i]=tolower(mname[i]);

}

fname[0] = toupper(fname[0]);

mname[0] = toupper(mname[0]);

rname = mname + ", " + fname;

}

  

cout << " Your formal name would be: " << rname;

cout << " Thanks for Formalizing with us Today!";

cout << " Have a pompous day!" << endl;

return 0;

}

Explanation / Answer

I have added some comments in the given code to make the code more self-explanatory.

1. To extract just the initial from the user's middle name the following lines of code are used:

if (fname.length()>0){
//Strip the name to get the middlename followed by the last name
name = name.substr(fname.length()+1);
//Strip the name to get only the middlename
mname = name.substr(0, name.find(' '));

}

mname[0] = toupper(mname[0]);    //Capitalize the first character

//Include only the first character of the middle name in the resultant formal name
rname = lname + ", " + fname + " "+ mname[0] + ".";

2. To make the user's middle name optional the following lines of code are used:

//If there's no middle name given
else
{
//Make all characters lower first
for(unsigned short i=0;i < fname.size();i++)
{

fname[i]=tolower(fname[i]);

}

for(unsigned short i=0;i < mname.size();i++)

{

mname[i]=tolower(mname[i]);

}

//make first character upper

fname[0] = toupper(fname[0]);

mname[0] = toupper(mname[0]);

rname = mname + ", " + fname;

}

Therefore it is okay not to use peek() finction.


3. What is wrong with the given code? How you might fix it?

Answer: In the line

   t = ' ' + s[i] + '.';

It will be treated as ASCII character addition instead of the desired string concatenation. So the ASCII values of ' ' (space char) and that of the char at s[i] and that of '.' (dot char) will be added and converted back to the corresponding ASCII char if it exists.

Fix is as follows

    t = ' ';
    t += s[i];
    t += '.';

Here we have done string concatenation with the binary '+' operator.

4. How did the user's name become properly capitalized?

Answer: It's because of the way the code is written in the following steps.

The relevant code is

//Make all characters lower first

for(unsigned short i=0;i < lname.size();i++)
{

lname[i]=tolower(lname[i]);

}


for(unsigned short i=0;i < fname.size();i++)
{

fname[i]=tolower(fname[i]);

}

for(unsigned short i=0;i < mname.size();i++)
{

mname[i]=tolower(mname[i]);

}

//make first character upper

lname[0] = toupper(lname[0]);

fname[0] = toupper(fname[0]);

mname[0] = toupper(mname[0]);




Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote