This lab will help you practice with function design, strings (i.e. variables/ob
ID: 3856138 • Letter: T
Question
This lab will help you practice with function design, strings (i.e. variables/objects of the string class), and loops. (There are also options about using vectors...) Write a program that reads a person's name in an informal notation/order (i.e. 'First Middle Last 'or 'First M. Last') and then outputs their name in a more formal notation/order: 'Last, First M.'. As an example, you might have the program interaction look something like (the parts in this color are typed by the user): $ /name_formal.out Welcome to the Name Formalizing Program!!! Enter your name: Mary Average User Your formal name would be: User, Mary A. Thanks for Formalizing with us today! Have a pompous day! $ /name_formal.out Welcome to the Name Formalizing Program!!! Enter your name: Mary A. User Your formal name would be: User, Mary A. Thanks for Formalizing with us today! Have a pompous day! $ /name_formal.out Welcome to the Name Formalizing Program!!! Enter your name: Mary User Your formal name would be: User, Mary Thanks for Formalizing with us today! Have a pompous day! $ /name_formal.outExplanation / Answer
NOTE: Please check the code below and let me know if you face any issues. I will revert back within 24 hours.
Code:
#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)
rname = lname + ", " + fname + " "+ mname[0] + ".";
else
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;
}
screenshot output:
https://pasteboard.co/GAxRxDF.png
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.