0

So the purpose of the program is to Create an array of 3 people, allow the user to populate the data in a for loop, ensure that the results are capitalized, and output the results. These new projects instructions were to 1. Rewrite capitalize() as a method within the structure. 2. Rewrite printPerson() as a method within the structure

The program itself works just fine, it's just not in the format that my professor wanted. He said the point of it is to not use any arguments but again, I don't know what he means. I just started programming a few months ago and even though I'm trying I don't have a strong knowledge of the terminology.

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

struct Person {
    string firstName;
    string middleName;
    string lastName;
    int age;
    string gender;

    void capitalize(Person &arg);
    void printPerson(Person arg);
};

Pretty sure these are the methods right here, but I'm not sure if the (person &arg) and (person arg) are the arguments itself or if they are parameters. I thought it was the "arg" part but I can't find a way to get the program to run without them as I'm pretty sure I need the & of operator to modify the information.

int main(void) {
   Person myPerson;
   Person a[3];
   const int size = 5;

   for (int i = 0; i <= 2; i++) {
       cout << "What is First Name #" << i + 1 << "? ";
       getline(cin, a[i].firstName);
       cout << "What is Middle Name #" << i + 1 << "? ";
       getline(cin, a[i].middleName);
       cout << "What is Last Name #" << i + 1 << "? ";
       getline(cin, a[i].lastName);
       cout << "Age #" << i + 1 << "? ";
       cin >> a[i].age;
       cin.ignore();
       cout << "Male or Female #" << i + 1 << "? ";
       getline(cin, a[i].gender);
       cout << endl;
    }

    for (int i = 0; i <= 2; i++) {
        myPerson.capitalize(a[i]);
        cout << "PERSON #" << i + 1 << endl;
        cout << "~~~~~~~~~~~~~~~" << endl;
        myPerson.printPerson(a[i]);
    }

    system("pause");
    return 0;
}

Along with that, I don't know how to manipulate the functions to work without the "parameters/arguments" (I'm not sure the difference at this point) or without the "arg"

void Person::capitalize(Person &arg) {
    transform(arg.firstName.begin(), arg.firstName.end(), arg.firstName.begin(), ::toupper);
    transform(arg.middleName.begin(), arg.middleName.end(), arg.middleName.begin(), ::toupper);
    transform(arg.lastName.begin(), arg.lastName.end(), arg.lastName.begin(), ::toupper);
}

void Person::printPerson(Person arg) {
    cout << "\nFirst Name: " << arg.firstName << endl;
    cout << "\nMiddle Name: " << arg.middleName << endl;
    cout << "\nLast Name: " << arg.lastName << endl;
    cout << "\nAge: " << arg.age << endl;
    cout << "\nGender: " << arg.gender << endl;
    cout << "\n\n";
}
tangoal
  • 724
  • 1
  • 9
  • 28
Jayus
  • 1
  • 1
    I would suggest that you [read some good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). This is very basic stuff. – ravnsgaard Nov 12 '18 at 22:20
  • "not use any arguments but again, I don't know what he means." You are saying you do not know what an 'argument' is? Did you try to look it up? For example, try looking up "argument c++". (either on-line or in a c++ book) – 2785528 Nov 12 '18 at 23:55

1 Answers1

0

The capitalize and the printPerson are now members (usually called methods) of the struct Person. This means that they operate on the member variables of an Person instance. Like this, you can just access all the classes members in these methods. See the following code. I also completed it with a constructor and made it slightly more readable.

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

struct Person {
public:
    Person();
    void readFromUserInput();
    void capitalize();
    void print();
public:
    string firstName;
    string middleName;
    string lastName;
    int age;
    string gender;
};

Person::Person() :
    firstName(""),
    middleName(""),
    lastName(""),
    age(0),
    gender("")
{

}

void Person::readFromUserInput()
{
    cout << "What is the First Name ? ";
    getline(cin, firstName);

    cout << "What is Middle Name ? ";
    getline(cin, middleName);

    cout << "What is Last Name ? ";
    getline(cin, lastName);

    cout << "Age ? ";
    cin >> age;
    cin.ignore();

    cout << "Male or Female ? ";
    getline(cin, gender);
}

void Person::capitalize() 
{
    transform(firstName.begin(), firstName.end(), firstName.begin(), ::toupper);
    transform(middleName.begin(), middleName.end(), middleName.begin(), ::toupper);
    transform(lastName.begin(), lastName.end(), lastName.begin(), ::toupper);
}

void Person::print() 
{
    cout << "\nFirst Name: " << firstName << endl;
    cout << "\nMiddle Name: " << middleName << endl;
    cout << "\nLast Name: " << lastName << endl;
    cout << "\nAge: " << age << endl;
    cout << "\nGender: " << gender << endl;
    cout << "\n\n";
}

int main(void)
{
    const int NUM_PERSONS = 3;
    Person a[NUM_PERSONS];

    for (int i = 0; i < NUM_PERSONS; i++)
    {
        cout << "### " << (i + 1) << ". User:" << endl;
        a[i].readFromUserInput();
        cout << endl;
    }

    for (int i = 0; i < NUM_PERSONS; i++) 
    {
        a[i].capitalize();
        cout << "PERSON #" << i + 1 << endl;
        cout << "~~~~~~~~~~~~~~~" << endl;
        a[i].print();
    }

    system("pause");
    return 0;
}
tangoal
  • 724
  • 1
  • 9
  • 28