Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to create a dynamic array for a circle class. I am not sure how to g

ID: 3848915 • Letter: I

Question

I am trying to create a dynamic array for a circle class. I am not sure how to go about this.

here is my code

void displayMenu();
int main() {
   float Radius;
   string Name, wantToFind, wantToDelete;
   int menuSelection;
   vector<Circle*> List_of_Circles;
   vector<Circle*>::iterator circleItr;
   do {
       Circle *SavedCircle = new Circle;
       displayMenu();
       cout << "Please enter your menu selection: ";
       cin >> menuSelection;
       switch(menuSelection){
           case 1:
               cout<< "Please enter the name of your circle: ";
               getline(cin, Name);
               cin.ignore(80, ' ');
               SavedCircle -> setName(Name);
               cout << "Please enter the radius: ";
               cin >> Radius;
               SavedCircle -> setRadius(Radius);
           List_of_Circles.push_back(SavedCircle);
           break;
           case 2:
               // Find a circle
               cout << "Which circle would you like to find? ";
               getline(cin, wantToFind);
               cin.ignore(80, ' ');
               for(int i = 0;i < List_of_Circles.size(); i++) {
                   if((SavedCircle->getName()) == wantToFind)
                        cout<<"The circle was found! :) ";
                   else if((SavedCircle->getName()) != wantToFind)
                       cout<<"The circle was not found. :(";
               }
           break;
           case 3:
               cout << "What circle would you like to delete?";
               getline(cin, wantToDelete);
               cin.ignore(80, ' ');
               for(circleItr=List_of_Circles.begin();
                    circleItr != List_of_Circles.end();) {
                     if((*circleItr)->getName()==wantToDelete)
                       {
                       delete (*circleItr);
                       circleItr = List_of_Circles.
                       erase(circleItr);
                       }
                   else {
                        cout << "The circle does not exist";
                       cout << " here" << endl;
                       circleItr ++;
                   }
               }

           break;
           case 4:
               cout << "CIRCLE INFORMATION" << endl;
               cout << "---------------------------------- ";
               cout << left << setw(12) << "NAME" << setw(12) << "DIAMETER"
                      << setw(12) << "AREA" << setw(12) << "CIRCUMFERENCE ";
               for(int x = 0; x!=List_of_Circles.size(); ++x)
               {
                   SavedCircle ->Display();
               }
           break;
           case 5:
               cout << "You are quitting the program. ";
           break;
           default:
               ;
       }
       delete SavedCircle;
   } while(menuSelection == 1 || menuSelection == 2 || menuSelection == 3
       || menuSelection == 4);

}
void displayMenu() {
   cout << "--------------------------------------------- ";
   cout << "1. Input a circle ";
   cout << "2. Find a circle ";
   cout << "3. Delete a circle ";
   cout << "4. Display all circles ";
   cout << "5. Exit ";
   cout << "---------------------------------------------- ";
}

class Circle {
   private:
   float radius;
   string name;
   public:
   // default constructor
   // an object has been initiated with a default radius of 1
   Circle();

   //default constructor
   // an object has been initiated with a default radius of r
   Circle(float r);
   //destructor
   // the object is destroyed
   ~Circle();
   void setName(string circleName);
   //set radius
   void setRadius(float r);
   // calculate area
   float getRadius();
   float CalculateArea();
   // calculate circumference
   float Circumference();
   // calculate diameter
   float Diameter();
   // print
   void Display();
   string getName();
};

//default constructor
//PRECONDITION: NONE
// POSTCONDITION: an object has been created and an automatic radius of one
//       has been initialized
Circle::Circle(){
   radius = 1.0;
}

//destructor
//PRECONDITION: an object exist with a valid radius
// POSTCONDITION: the object is destroyed
Circle::~Circle(){
}

// Set Radius
// PRECONDITION: an object exists with a valid radius
//POSTCONITION: the radius is changed to r
void Circle :: setRadius(float r){
   radius = r;
}
//Gets radius
// PRECONITION: an object exists with a valid radius
//POSTCONDITION: the radius is changed to r
float Circle :: getRadius(){
   return radius;
}
// Calculate Area
//PRECONITION: an object with a valid radius exist
// POSTCONITION: calculates and returns the area of the circle
float Circle :: CalculateArea() {
   return (PI * (radius*radius));
   }
//Calculate the cirumference
//PRECONDITION: an object exist with a valid radius
//POSTCONDITION: calculates and returns the circumerence of the circle
float Circle :: Circumference() {
   return (2 * PI * radius);
   }

//Calculate Diameter
// PRECONDTION: an object exists with a valid radius
//POSTCONDITION: returns the diameter of the circle
float Circle :: Diameter(){
   return (2 * radius);
}

void Circle :: setName(string circleName){
   name = circleName;
}
string Circle :: getName() {
   return name;
}
// Display
//PRECONDITION: an object exists with a valid radius
//POSTCONDITION: the diameter, circuference, area and circle are displayed
void Circle :: Display () {
       cout << left << setprecision(3)<< fixed << setw(12) << name
              << setw(12) << Diameter() << setw(12) << CalculateArea()
              << setw(12) << Circumference()<< endl;
   }

This vector is only storing the last element in the array, how do I fix this?

Explanation / Answer

1. Program in question is creating a new circle object everytime the do while loop is executing. This should not be the case. => Need to remove code creating new object every time. (Circle *SavedCircle = new Circle;)

2. Within switch statement, Circle object needs to be created only once when we are adding a circle object to the vector. (Here case 1). So add Circle *SavedCircle = new Circle; here.

3. In case 2, where we are expected to find a circle from the list of vectors if available. We need to check every element to find whether Circle we are searching for is present or not. In your code, we are making use of SavedCircle, which is newly allocated object (nothing to do with search as it is newly created and is not correct as mentioned in point 1 and 2)

You can just loop through just like arrays.

4. I see that you are deleting SavedCircle each time after switch statement. The effect of this will be -

i. You are allocating memory to a circle object (case 1 of switch statement)

ii. You are adding the pointer of that object to vector

iii. You are deleting that pointer (Meaning you are deleting object)

iv. So at the end of an iteration of while loop you will have a dangling pointer added to your vector.

Dangling pointer means: A pointer which is not pointing to any valid memory (object) location.

The above mistakes I have seen and I corrected the code and pasting the altered main(){} here. I have also added comments at appropriate place with prefix Expert. Please remove or edit comments if needed.

Please check: Thanks!!

int main() {
float Radius;
string Name, wantToFind, wantToDelete;
int menuSelection;
vector<Circle*> List_of_Circles;
vector<Circle*>::iterator circleItr;
do {
   /* Expert: Dont need to allocate circle object here */  
displayMenu();
cout << "Please enter your menu selection: ";
cin >> menuSelection;
switch(menuSelection){
case 1:
cout<< "Please enter the name of your circle: ";
getline(cin, Name);
cin.ignore(80, ' ');
       /* Expert: Create saved circle object here. Since creation is needed only if we are adding new circle */      
       Circle *SavedCircle = new Circle;      
SavedCircle -> setName(Name);
cout << "Please enter the radius: ";
cin >> Radius;
SavedCircle -> setRadius(Radius);
List_of_Circles.push_back(SavedCircle);
break;
case 2:
// Find a circle
cout << "Which circle would you like to find? ";
getline(cin, wantToFind);
cin.ignore(80, ' ');

       /* Expert: You need to go through the list pointing to each object.
       Not using Saved Circle Object. So made some code changes here*/
       int circleFound = 0; /* Mark circle not available and check the list*/
              
for(int i = 0;i < List_of_Circles.size(); i++)
       {
       /* Expert: Go through the complete list and check if circle is found */
   if((List_of_Circles[i]->getName()) == wantToFind)
   {
   cout<<"The circle was found! :) ";
               /*Circle found. Mark circle as found and break as we dont need to check other circle objects*/
               circleFound = 1;
               break;
   }
} /* End of for loop */

/*circleFound will be 0 if we dont find the circle in the loop above*/
      if(circleFound == 0)
{
   cout<<"The circle was not found. :(";
}

break;
case 3:
cout << "What circle would you like to delete?";
getline(cin, wantToDelete);
cin.ignore(80, ' ');
for(circleItr=List_of_Circles.begin();
circleItr != List_of_Circles.end();) {
if((*circleItr)->getName()==wantToDelete)
{
delete (*circleItr);
circleItr = List_of_Circles.
erase(circleItr);
}
else {
cout << "The circle does not exist";
cout << " here" << endl;
circleItr ++;
}
}
break;
case 4:
cout << "CIRCLE INFORMATION" << endl;
cout << "---------------------------------- ";
cout << left << setw(12) << "NAME" << setw(12) << "DIAMETER"
<< setw(12) << "AREA" << setw(12) << "CIRCUMFERENCE ";
for(int x = 0; x!=List_of_Circles.size(); ++x)
{
SavedCircle ->Display();
}
break;
case 5:
cout << "You are quitting the program. ";
break;
default:
;
}
   /*Expert: You should not delete SavedCircle pointer as it will delete memory allocated for the object in vector.
   If you do so, you are allocating memory to vector objects and deleting. In other words, vector will have dangling pointers */  
// Dont do this => delete SavedCircle;
} while(menuSelection == 1 || menuSelection == 2 || menuSelection == 3
|| menuSelection == 4);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote