Question: 1. Program in question is creating a new circle object everytime the do while loop is executing. This should not be the case. => Need
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
/* 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); }
This is my code, but when I run the program it does not show the name of the circle when I chose to display. How can I fix that? ALso how can I make sure that it deletes the correct circle because with this code it is not
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
