0

This is what I have so far, and it does give me the desired output, but I couldn't figure out how to use a dynamically allocated array. I wanted to figure out the problem without that first, so I can figure out how the rest of the code would work.

Instead of using a constant variable for the size of the array, how can I have the user input a desired amount that would then be used for the array?

class Person
{
private:
  string name;
  int age;

public:
  // Default constructor
  Person()
  {
    name = " ";
    age = 0;
  } 

  Person(string name1, int age1)
  {
     name = name1;
     age = age1;
  }
  
  int getAge() { return age; }
  string getName() { return name; }
  
  // Mutator functions to set name and age
  void setName(string name1);
  void setAge(int age1);
};

int lengthOfName(Person *p);

// Main Function
int main()
{   
  string name;
  int age;
  const int SIZE = 3;

  Person personArray[SIZE];

  for (int i = 0; i < SIZE; i++)
  {
    cout << "Enter the name:" << endl;
    cin >> name;
    personArray[i].setName(name);

    cout << "Enter the age:" << endl;
    cin >> age;
    personArray[i].setAge(age);
  }

  for (int i = 0; i < SIZE; i++)
  {
      cout << "The name " << personArray[i].getName();
      cout << " has length: " << lengthOfName(&personArray[i]) << endl;
  }
}

// Returns the number of characters in a person's name *
int lengthOfName(Person *p)
{
  string name = p->getName();
  return name.length();
}

// Mutator function that sets variable name
void Person::setName(string name1)
{
  name = name1;
}

// Mutator function that sets variable age
void Person::setAge(int age1)
{
  age = age1;
}
Chris
  • 26,361
  • 5
  • 21
  • 42
akaN26
  • 1

1 Answers1

1

Dynamic arrays are allocated using the new[] operator, and freed using the delete[] operator, eg:

size_t size;
cin >> size;

Person *personArray = new Person[size];
...
delete[] personArray;

However, you should use std::vector instead and let it manage the memory for you:

#include <vector>

size_t size;
cin >> size;

std::vector<Person> personArray(size);
...
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • @Peter This exercise becomes so much simpler when there are only -639987340 people on the earth. :) – Goswin von Brederlow May 28 '22 at 00:24
  • @GoswinvonBrederlow Yes, indeed. Having a user enter a value for `size` of `-1` and either allocating a huge amount of memory or having the program terminate by throwing an exception- is such a great learning exercise for a beginner as well. ;-) – Peter May 28 '22 at 00:32
  • Thanks a lot everyone, this helped because I tried playing around with the new[] operator but couldn't figure it out but it was because I wasn't using size_t. – akaN26 May 28 '22 at 00:43
  • He is requiring us to use pointers because that is the section that this problem is from but using vector seems really useful. – akaN26 May 28 '22 at 00:44
  • 1
    @akaN26 Welcome to yet another "You wanted to learn C++ so we teach you C first" class. Raw pointers should be one of the last things you learn. They are to be avoided in c++. But since you must use them look into `std::unique_ptr`, `std::shared_ptr`, `std::make_unique` and `std::make_shared`. If you can avoid using `new` directly you have already won half the battle. – Goswin von Brederlow May 28 '22 at 00:50
  • @akaN26 "*[I] couldn't figure it out but it was because I wasn't using size_t*" - `size_t` is not the secret that makes the magic work. `int` works just as well, as long as the value is `> 0`. "*He is requiring us to use pointers because that is the section that this problem is from*" - just know that you can get the pointer that the vector holds (via the `vector::data()` method), if you need it. But modern C++ best practices actually *avoid* using raw pointers directly, except for *non-owning* views of data. – Remy Lebeau May 28 '22 at 04:07
  • @GoswinvonBrederlow I did learn C first. But C++ makes it so much easier to do things you had to contort in C. The problem is teaching C++ with a too short background in C. Once you are good at C and get a chance to move up, you really, really appreciate C++. – doug May 28 '22 at 04:52
  • @doug You don't really need all that background in C till you are advanced in C++ and it scares off too many people from using C++. Plus it's just so much more to type. – Goswin von Brederlow May 28 '22 at 15:09
  • @GoswinvonBrederlow I realized that the code does not work when I enter a name with a space, ie. "Joe Somebody", it only account for the first string and it doesn't count the space and the last name. Is there a way i could fix this? – akaN26 May 28 '22 at 22:40
  • @akaN26 use `std::getline()` instead of `operator>>`. See https://stackoverflow.com/questions/5838711/ – Remy Lebeau May 28 '22 at 22:41
  • @Remy Lebeau I used it but now it doesn't read anything – akaN26 May 28 '22 at 22:46
  • I figured it out by using "std::getline(std::cin >> std::ws, name);" , it was on the same forum thanks again! – akaN26 May 28 '22 at 23:00
  • @akaN26 sounds like you likely ran into [this issue](https://stackoverflow.com/questions/21567291/) – Remy Lebeau May 28 '22 at 23:11
  • @akaN26 `int` does not work well when someone enters the number of people alive on earth right now (estimate from this morning). Because that becomes -639987340. You seem to have missed the connection. – Goswin von Brederlow May 28 '22 at 23:32