Question: Please correct errors in C++ These are the changes that need to be implemented You need to use cin.fail() anytime that you enter in any
Please correct errors in C++
These are the changes that need to be implemented
You need to use cin.fail() anytime that you enter in any numerical input. You need to make sure that the user has to input any number greater than 0 for the count. You need to make sure that the user can't skip the input for entering in the animal type. Also, I removed the code for deleting the arrays and setting the pointers back to null; you should add that code back in.
This is part of the code that needs to be implemented.
while (cin.fail()) { cin.clear(); cin.ignore(32767, ' '); cout << "Invalid data-type! Try to enter a number between 1 and 3: "; cin >> MenuChoice; }
This is my code
#include
// structure to store the details struct Animal { string animalsName = ""; int animalQuantity = 0; };
// defining show options void addAnimals(Animal Animals[], int); void displayAnimals(Animal[], int);
// to store the index int indexNumber = 0;
void addAnimals(Animal Animals[], int numberOfRecords) { // initializing struct values
if (indexNumber < numberOfRecords) { while (indexNumber < numberOfRecords) { string animalType, animalTypeLowercase = "";
cin.ignore();
cout << "Please enter an animal type (none to stop): "; getline(cin, animalType);
// converting animalType to lowercase for (int i = 0; i < animalType.length(); i++) { animalTypeLowercase += tolower(animalType[i]); }
// validating input if (animalTypeLowercase == "none") { return; } else { Animals[indexNumber].animalsName = animalType; // storing animal type
// asking for count input int animalTypeCount; cout << "Enter the animal type's count: "; cin >> animalTypeCount; Animals[indexNumber].animalQuantity = animalTypeCount; // storing animal count }
indexNumber++; // incrementing index number } return; // showing options when all the data is filled } else if (indexNumber >= numberOfRecords) { cout << endl << "Records are already full" << endl; return; } }
void displayAnimals(Animal Animals[], int numberOfRecords) {
if (indexNumber > 0) { for (int i = 0; i < indexNumber; i++) { cout << endl; cout << "Animal: " << Animals[i].animalsName << endl; cout << "Count: " << Animals[i].animalQuantity << endl; } return; } else { cout << endl << "No records yet" << endl; return; } }
int main() {
int numberOfRecords; // to store the number of records
// asking for input cout << "How many animal records you would like to store (2-5): "; cin >> numberOfRecords;
do {
if (numberOfRecords > 5 || numberOfRecords < 2) {
cout << endl << "Invalid choice. Try again: "; cin >> numberOfRecords;
while (cin.fail()) { cin.clear(); cin.ignore(32767, ' '); cout << "Invalid data-type! Try again: "; cin >> numberOfRecords; }
}
} while (numberOfRecords > 5 || numberOfRecords < 2);
Animal* Animals = new Animal[numberOfRecords];
int response = 0;
while (response != 3) { cout << endl << endl; cout << "1. Add animal(s)" << endl; cout << "2. Display animals" << endl; cout << "3. Quit" << endl;
// getting the response
cout << endl << "Menu choice: "; cin >> response;
// validating response if (response == 1) { addAnimals(Animals, numberOfRecords); } if (response == 2) { displayAnimals(Animals, numberOfRecords); } if (response == 3) {
// displaying exiting message cout << "Exiting..." << endl; }
}
return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
