dimanche 9 février 2020

How Do I Deal With This C++ Multimap Issue?

I am a beginner in C++ working on simple program and I ran into an issue which left me stumped... Basically I created a multimap that accepts a part of string person name and a pointer to a person object.

So My first function to work with was the "Add()" function. I basically construct a new person with a name and age and then set a pointer to that object. Then I insert the name and the object into the multimap as a pair.

The problem happens when I run the find() function... My output only gives me a valid name but for the age I get what seems like a truncated memory address or a random number? Since i'm a beginner I am pretty sure I am doing something very simple yet very stupid. I don't think you will need to run the code since it's so simple. All help is appreciated. Thank you!

My runnable file:

#include "Person.h"
#include <map>
using namespace std;

multimap<string, Person*> personMap;
multimap<string, Person*>::iterator counter;


void add()
{
  string name;
  int age;
  cout << "Please enter the name of the person: ",  cin >> name;
  cout << "Please enter the age of the person: ", cin >> age;
  Person generatedPerson(name, age);

  // Avoid dynamic class pointers for memory optimization.
  // Avoided: Person* pointer = new Person(name, age).
  Person *pointer = &generatedPerson;
  personMap.insert({name, pointer});

}

void find()
{
  string personToBeFound;
  cout << "Who do you wish to find, friend: ", cin >> personToBeFound;


  for (counter = personMap.begin(); counter != personMap.end(); counter++)
  {
    if (counter->first == personToBeFound)
    {
      cout << "\nName: " << counter->first << " Age: " << counter->second->getAge() << endl;
    }
    else if (counter->first != personToBeFound)
    {
      cout << "Error 404, person does not exist..." << endl;
    }
  }
}

// Experimental....
int main(int argc, char* argv[])
{
  int menuChoice = -1;

  while (menuChoice != 0)
  {
    cout << "\nPlease enter: "
            "\n1 - to add a person "
            "\n2 - to find a person"
            "\n0 - to quit\n" << endl;
    cin >> menuChoice;
    switch(menuChoice)
    {
      case 1: add();
        break;
      case 2: find();
        break;
      case 0: menuChoice = 0;
    }
  }
}

HEADER FILE:

#ifndef PERSON_H
#define PERSON_H

#include <string>
#include <vector>
#include <iostream>

class Person {
 public:
  // Constructors

  /**
   * Create a Person with the given name and age.
   *
   * @param name name of the person
   * @param age age of the person - defaults to 0
   */
  Person(const std::string& name, unsigned short age = 0);


  // No explicit destructor necessary


  // Mutators

  /**
   * Set the name attribute
   *
   * @param name name of the person
   */
  void setName(const std::string& name);

  /**
   * Set the age attribute
   *
   * @param age age of the person
   */
  void setAge(unsigned short age);

  /**
   * Increment the age attribute
   */
  void growOlder();

  /**
   * Add a person to our list of children
   *
   * @param child Person to add as a child
   */
  void addChild(const Person& child);


  // Accessors

  /**
   * @return the Person's name
   */
  const std::string& getName() const;

  /**
   * @return the Person's age
   */
  unsigned short getAge() const;

  /**
   * @return a list of this Person's children
   */
  const std::vector<const Person *>& getChildren() const;

  /**
   * Define the ostream's << operator as a friend of this class
   * to allow this object to be printed to an output stream
   *
   * @param output the stream to print to
   * @param p the Person to print
   *
   * @return the output stream printed to
   */
  friend std::ostream& operator<< (std::ostream& output, const Person& p);


 private:

  // 0-arg Constructor
  Person();

  // Private attributes
  std::string _name;
  unsigned short _age;
  std::vector<const Person *> _children;

}; // Person

#endif

METHOD DEFINITIONS:

#include "Person.h"

Person::Person(const std::string& name, unsigned short age) :
    _name(name) , _age(age) {
}


void Person::setName(const std::string& name) {
  _name = name;
}


void Person::setAge(unsigned short age) {
  _age = age;
}


void Person::growOlder() {
  _age++;
}


void Person::addChild(const Person& child) {
  _children.push_back(&child);
}


const std::string& Person::getName() const {
  return _name;
}


unsigned short Person::getAge() const {
  return _age;
}


const std::vector<const Person *>& Person::getChildren() const {
  return _children;
}


std::ostream& operator<< (std::ostream& output, const Person& aPerson) {
  // Print our attributes to the output stream
  return output << "Name: '" << aPerson._name << "', Age: " << aPerson._age;
}

Aucun commentaire:

Enregistrer un commentaire