c++ Design and define a Person structure. Each person has a first name, last nam
ID: 3605968 • Letter: C
Question
c++
Design and define a Person structure. Each person has a first name, last name, and age Declare an array of 3 persons. Allow the user to populate the array Write a function that ensures that the names are capitalized. The prototype should look like: void capitalize (Person &arg;) Q: Explain why we need the address-of operator (&). Answer in a comment within the function. Write a function that nicely outputs the data encapsulated within the Person objects. The prototype might look like void printPerson (Person arg) i Q Explain why we do not need the address-of operator (&) Answer in a comment within the function. Within the main function choose an appropriate repetition structure to loop the 3 function calls that will print out the dataExplanation / Answer
The explanation for the code is given with the function as requested in the question. The struct Person is used and an array of 3 persons is created. 2 loops are used to input the data and print out the data.
capitalize function converts the names to capital using toupper function.
If you have any more doubts.. Kindlty comment below.
*********************************************************************************************************************************************
Code:
#include <iostream>
#include <algorithm>
using namespace std;
struct Person
{
std::string firstName;
std::string lastName;
int age;
};
void capitalize(Person &arg)
{
/*Here & address-of-operator is required because, we need to capitalize the the data.
If the address of the Person would not have been passed, the data in the person would be changed locally but would'nt affect
it in the memory. So, we pass the address of the Person and then cj=hange it in the memory directly.*/
transform(arg.firstName .begin(), arg.firstName .end(), arg.firstName .begin(), ::toupper);
transform(arg.lastName .begin(), arg.lastName .end(), arg.lastName .begin(), ::toupper);
}
void printPerson(Person arg)
{
/*Here in this function the function does not need the data to be modified, so it accepts the
data by call by value and just prints it. Data is not required to be changed, thus
address-of-operator is not required*/
cout << " First Name: " << arg.firstName << endl;
cout << "Last Name: " << arg.lastName << endl;
cout << "Age: " << arg.age << endl;
cout << " ";
}
int main()
{
Person persons[3];
for(int i=0;i<3;i++)
{
cout << "Enter first name for Person" << i+1 << endl;
cin >> persons[i].firstName;
cout << "Enter last name for Person" << i+1 << endl;
cin >> persons[i].lastName;
cout << "Enter age for Person" << i+1 << endl;
cin >> persons[i].age;
}
for(int i=0;i<3;i++)
{
capitalize(persons[i]);
cout << "Person" << i+1 << endl;
printPerson(persons[i]);
}
return 0;
}
********************************************************************************************************************************************
Sample outputs:
***********************************************************************************************************************************************
I hope this helps you.
If you find my answer helpful,
Kindly rate the answer.
All the best :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.