Question: My Program has a few errors please fix them using C++ These are the errors 1. Do not do this: typedef struct Animal { string

My Program has a few errors please fix them using C++

These are the errors

1. Do not do this:

typedef struct Animal { string type = ""; int count = 0; }animal;

use this style:

struct Animal { string type = ""; int count = 0; };

2. It is a poor programming practice to use while (true). Always use a boolean expression. We never teach this in our Computer Science curriculum.

while (1)

3. Do not use one-letter identifiers for your variables and data structures.

Always use descriptive identifiers.

4. This should be in the toLowerCase function:

for (int i = 0;i < type.length();i++) { type[i] = tolower(type[i]); }

This is my code

#include #include #include #include

using namespace std; typedef struct Animal { string type = ""; int count = 0; }animal;

bool isNumber(string& ); // function prototype

//function prints a menu and read the user selection and returns the same int menu() { int choice = -1; string temp; while (1) { //iterate until user does not select a valid input cout << "1. Add animal(s)" << endl; cout << "2. Display animals" << endl; cout << "3. Quit" << endl; cout << endl; cout << "Menu choice: "; getline(cin, temp);

if (isNumber(temp)) //if user input a valid number choice = stoi(temp);

if (choice >= 1 && choice <= 3) //if selection is valid return choice; //otherwise cout << "Invalid Selection! Try again." << endl << endl; } } //adds an animal with type t and count c to vector v void addAnimal(string t, int c, vector& v) { animal a; a.type = t; a.count = c; v.push_back(a); } //main function int main() { int choice = -1; //to store the user choice vector v; //vector to keep the animal structs while (choice != 3) { //iterate while user does not wants to quit choice = menu(); //print and read what user selected switch (choice) { //process user selection case 1: { while (1) {//keep adding the animals while user doesnt enter "NONE" string type = ""; int count = 0;

if (v.size() == 5) {//if vector size is 5 then stop adding cout << endl; break; }

cout << "Please enter an animal type (none to stop): "; getline(cin, type); //convert the type to lower case for (int i = 0;i < type.length();i++) { type[i] = tolower(type[i]); }

if (type == "none") {//if user enters nont then exit then dont add this cout << endl; break; }

cout << "Enter the animal type's count: "; string temp; //to keep the string version of count while (1) { getline(cin, temp); if (isNumber(temp)) {//if temp is numeric convert and store it in count and add the animal count = stoi(temp); break; } else {//count is not numberic print error and read again cout << "Invalid count! Try again: "; } } addAnimal(type, count, v); } break; } case 2://print the vector cout << endl; for (std::vector::const_iterator i = v.begin(); i != v.end(); ++i) { cout << "Animal: " << (i->type) << endl; cout << "Count: " << (i->count) << endl; cout << endl << endl; } break; case 3://exit break; } } return 0; }

bool isNumber(string& s) { for (int i = 0;i < s.length();i++) { if (!isdigit(s.at(i))) //if atleast one character is not numberic return false return false; } return true; //we passed all tests that means its a number }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!