Write a program that request a student\'s name in the following form: lastName,f
ID: 3551058 • Letter: W
Question
Write a program that request a student's name in the following form: lastName,firstName. The program then converts each name to the following form: firstName lastName. Your program must read the student's entire name in a variable and must consist of a convertName function that takes as input a string, consists of a student's name, and returns the string consisting of the altered name. Use the string function find to find the index of a ","; the function length to find the length of the string; and the function substr to extract the firstName, and lastNameThis is what I have so far... I am not sure if i am doing this right or how to use the ConvertName function...
using namespace std;
int main() {
string firstName, lastName, fullName, chgName;
string line;
cout << "Enter your last name and first name separeted by a comma" ;
cin >> fullName;
cout <<" You entered: " << fullName << endl;
int comma = line.find(',');
int length = line.length();
lastName = line.substr(0,comma);
firstName = line.substr(comma+2, length);
}
Write a program that request a student's name in the following form: lastName,firstName. The program then converts each name to the following form: firstName lastName. Your program must read the student's entire name in a variable and must consist of a convertName function that takes as input a string, consists of a student's name, and returns the string consisting of the altered name. Use the string function find to find the index of a ","; the function length to find the length of the string; and the function substr to extract the firstName, and lastName
This is what I have so far... I am not sure if i am doing this right or how to use the ConvertName function...
using namespace std;
int main() {
string firstName, lastName, fullName, chgName;
string line;
cout << "Enter your last name and first name separeted by a comma" ;
cin >> fullName;
cout <<" You entered: " << fullName << endl;
int comma = line.find(',');
int length = line.length();
lastName = line.substr(0,comma);
firstName = line.substr(comma+2, length);
}
Write a program that request a student's name in the following form: lastName,firstName. The program then converts each name to the following form: firstName lastName. Your program must read the student's entire name in a variable and must consist of a convertName function that takes as input a string, consists of a student's name, and returns the string consisting of the altered name. Use the string function find to find the index of a ","; the function length to find the length of the string; and the function substr to extract the firstName, and lastName
This is what I have so far... I am not sure if i am doing this right or how to use the ConvertName function...
using namespace std;
int main() {
string firstName, lastName, fullName, chgName;
string line;
cout << "Enter your last name and first name separeted by a comma" ;
cin >> fullName;
cout <<" You entered: " << fullName << endl;
int comma = line.find(',');
int length = line.length();
lastName = line.substr(0,comma);
firstName = line.substr(comma+2, length);
}
Explanation / Answer
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
struct nameType {
string fname;
string mname;
string lname;
nameType() {reset();}
void reset()
{
fname = "";
mname = "";
lname = "";
}
void print(ostream& os)
{
os << fname;
if (mname.length() > 0) os << " " << mname;
if (lname.length() > 0) os << " " << lname;
cout << endl;
}
};
void split_line(const string& strLine, nameType& dest)
{
size_t pos_b = 0, pos_c = 0;
dest.reset();
pos_b = strLine.find(' ');
if (pos_b != string::npos) {
dest.fname = strLine.substr(0, pos_b);
pos_c = strLine.find(' ', pos_b + 1);
if (pos_c != string::npos) {
dest.mname = strLine.substr(pos_b + 1, pos_c - pos_b - 1);
dest.lname = strLine.substr(pos_c + 1);
}
else {
dest.mname = "";
dest.lname = strLine.substr(pos_b + 1);
}
}
else {
dest.fname = strLine;
dest.mname = "";
dest.lname = "";
}
}
int main()
{
string name;
size_t pos_a, pos_b, pos_c;
nameType nt;
while(true){
nt.reset();
cout << "Enter Student's name> ";
getline(cin, name);
split_line(name, nt);
nt.print(cout);
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.