Question

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

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 << "\t\tWelcome to the name Formalizing program!!!";

cout << "\nEnter 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 << "\nYour formal name would be: " << rname;

cout << "\nThanks for Formalizing with us Today!";

cout << "\nHave a pompous day!" << endl;

return 0;

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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.

  • Firstly, all the characters are made lowercase.
  • The first character of each of the first name, middle name (if any), last name are capitalized.

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]);




Add a comment
Know the answer?
Add Answer to:
With relation to the code I have given below can you guys help me answer these...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

  • The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried t...

    The following C++ code include 3 files: Patient.h, Patient.cpp and Main.cpp. The program basically creates and stores patient records. The original code has everything in a single .cpp file. I tried to divide the code in 3 parts (Patient.h, Patient.cpp and Main.cpp), but it is giving me errors. Patient.h #ifndef PATIENT_H #define PATIENT_H #include <string> #include "Patient.cpp" using namespace std; class Patient{ private : string firstname; string lastname; string location; static int cnt; int id; public : Patient(string, string, string);...

  • The code will not run and I get the following errors in Visual Studio. Please fix the errors. err...

    The code will not run and I get the following errors in Visual Studio. Please fix the errors. error C2079: 'inputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' cpp(32): error C2228: left of '.open' must have class/struct/union (32): note: type is 'int' ): error C2065: 'cout': undeclared identifier error C2065: 'cout': undeclared identifier error C2079: 'girlInputFile' uses undefined class 'std::basic_ifstream<char,std::char_traits<char>>' error C2440: 'initializing': cannot convert from 'const char [68]' to 'int' note: There is no context in which this conversion is possible error...

  • Can someone help me fill in the first part of this c++ code: I need it...

    Can someone help me fill in the first part of this c++ code: I need it to print the menu based on the day selected. #include #include using namespace std; int main(){ double sub_total=0,tax=0,total=0; string food1[3]={"T-Bone Steak","Pork Chops","Iceland Cod"}; double prices_f1[]={20.50,15.45,10.55}; string food2[3]={"Sirloin Steak","Salmon Fillet","Jumbo Shrimp"}; double prices_f2[]={30.35,24.50,15.50}; string food3[3]={"Pork Tenderloin","Buffalo Chicken Sandwhich","Avocado Burger"}; double prices_f3[]={10.60,15.60,25.60}; string drink1[3]={"Raspberry Sgroppino","Sparkling Apple Sangria ","Spiced Cranberry Rum"}; double prices_d1[]={2.55,5.75,4.25}; string drink2[3]={"Champagne Mojitos","Roman Hoilday Cocktail","Dry Martini"}; double prices_d2[]={1.5,3,25,8.45}; string drink3[3]={"Radler Beer","Port wine","Primm's"}; double prices_d3[]={3.55,2.25,4.45};...

  • Hi, I want to creat a file by the name osa_list that i can type 3...

    Hi, I want to creat a file by the name osa_list that i can type 3 things and store there ( first name, last name, email). then i want to know how to recal them by modifying the code below. the names and contact information i want to store are 200. #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> using namespace std; class Student { private: string fname; string lname; string email;       public: Student(); Student(string fname, string lname,...

  • Fix my code, if I the song or the artist name is not on the vector,...

    Fix my code, if I the song or the artist name is not on the vector, I want to user re-enter the correct song or artist name in the list, so no bug found in the program #include <iostream> #include <vector> #include <string> #include <algorithm> using namespace std; class musicList{ private: vector<string> songName; vector<string> artistName; public: void addSong(string sName, string aName){ songName.push_back(sName); artistName.push_back(aName); } void deleteSongName(string sName){ vector<string>::iterator result = find(songName.begin(), songName.end(), sName); if (result == songName.end()){ cout << "The...

  • (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the...

    (Coding done in c++, Virtual Studio) Having a problem with the line trying to compare the date of birth.            **fall.sort(compare_DOB); //Here lies your error** #include <fstream> #include <iostream> #include <string> #include <list> #include <cctype> using namespace std; const char THIS_TERM[] = "fall.txt"; const string HEADER = "\nWhat do you want to do ?\n\n" "\t. Add a new friend name and DOB ? \n" "\t. Edit/Erase a friend record ? \n" "\t. Sort a Friend's record ? \n"...

  • Write a simple telephone directory program in C++ that looks up phone numbers in a file...

    Write a simple telephone directory program in C++ that looks up phone numbers in a file containing a list of names and phone numbers. The user should be prompted to enter a first name and last name, and the program then outputs the corresponding number, or indicates that the name isn't in the directory. After each lookup, the program should ask the user whether they want to look up another number, and then either repeat the process or exit the...

  • Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem...

    Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem to get my code to work. The output should have the AVEPPG from highest to lowest (sorted by bubbesort). The output of my code is messed up. Please help me, thanks. Here's the input.txt: Mary 15 10.5 Joseph 32 6.2 Jack 72 8.1 Vince 83 4.2 Elizabeth 41 7.5 The output should be: NAME             UNIFORM#    AVEPPG Mary                   15     10.50 Jack                   72      8.10 Elizabeth              41      7.50 Joseph                 32      6.20 Vince                  83      4.20 ​ My Code: #include <iostream>...

  • Can someone please tell me why the calculation for earnings is not coming through at the...

    Can someone please tell me why the calculation for earnings is not coming through at the end of the program? Thank you. //This program should tell us about streaming services #include <iostream> #include <iomanip> #include <string> using namespace std; int main() {    int choice, streams;    double earnings;    string song;    string artist;    string streamingService;       const int Tidal = 0.01250;    const int Amazon = 0.00402;    const int Apple_Music = 0.00735;    const int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT