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

#include<iostream> #include<vector> #include<cstdlib> #include<sstream> #include

ID: 3588915 • Letter: #

Question

#include<iostream>

#include<vector>

#include<cstdlib>

#include<sstream>

#include<fstream>

using namespace std;

class Animal

{

public:

Animal()

{

}

~Animal()

{

}

virtual void read(vector<string>) = 0;

virtual void write() = 0;

string getName()

{

return name;

}

string getColor()

{

return color;

}

void setName(string name)

{

this->name = name;

}

void setColor(string color)

{

this->color = color;

}

private:

string name;

string color;

};

class Dog : public Animal

{

private:

string breed;

int age;

double weight;

public:

double subtract10(double weight)

{

if (weight > 10)

return weight - 10;

else

throw exception("Weight can not be negative");

}

void read(vector<string> in)

{

//string name,string color,string breed,int age,double weight

this->setName(in[0]);

this->breed = in[1];

this->age = atoi(in[2].c_str());

this->setColor(in[3]);

this->weight = strtod(in[4].c_str(), NULL);

}

void write()

{

cout << this->getName() << " " << this->breed << " " << this->age << " " << this->getColor() << " " << subtract10(this->weight) << endl;

}

};

class Fish : public Animal

{

private:

string freshwater;

string habitat;

string predator;

public:

void read(vector<string> in)

{

this->setName(in[0]);

this->setColor(in[1]);

this->freshwater = in[2];

this->habitat = in[3];

this->predator = in[4];

}

void write()

{

cout << this->getName() << " " << this->getColor() << " " << this->freshwater << " " << this->habitat << " " << this->predator << endl;

}

};

class Horse : public Animal

{

private:

string maneColor;

int age;

double height;

public:

int add1(double h)

{

height = height + 1;

return height;

}

void read(vector<string> in)

{

this->setName(in[0]);

this->setColor(in[1]);

this->maneColor = in[2];

this->age = atoi(in[3].c_str());

this->height = strtod(in[4].c_str(), NULL);

}

void write()

{

cout << this->getName() << " " << this->getColor() << " " << this->maneColor << " " << this->age << " " << add1(this->height) << endl;

}

};

class Monkey : public Animal

{

private:

int age;

string wild;

string home;

string endangered;

public:

string changeEndangered()

{

if (endangered == "yes")

endangered = "no";

else

endangered = "yes";

return endangered;

}

void read(vector<string> in)

{

this->setName(in[0]);

this->setColor(in[1]);

this->wild = in[3];

this->age = atoi(in[2].c_str());

this->home = in[4];

this->endangered = in[5];

}

void write()

{

cout << this->getName() << " " << this->getColor() << " " << this->age << " " << this->wild << " " << this->home << " " << changeEndangered() << endl;

}

};

class Lizard : public Animal

{

private:

string habitat;

string Isprotected;

double weight;

public:

void read(vector<string> in)

{

this->setName(in[0]);

this->setColor(in[1]);

this->habitat = in[2];

this->Isprotected = in[3];

this->weight = strtod(in[4].c_str(), NULL);

}

void write()

{

cout << this->getName() << " " << this->getColor() << " " << this->habitat << " " << this->Isprotected << " " << this->weight << endl;

}

};

//tokenizer

vector<string> split(const string &s, char delim) {

stringstream ss(s);

string item;

vector<string> tokens;

while (getline(ss, item, delim)) {

tokens.push_back(item);

}

return tokens;

}

int main()

{

string line;

ifstream dog("Dog.txt");

if (dog.is_open())

{

while (getline(dog, line))

{

//cout << line << ' ';

vector<string> tokens = split(line, ', ');

Dog g;

g.read(tokens);

g.write();

}

dog.close();

}

else cout << "Unable to open file";

system("pause");

return 0;

}

Dog.txt: Max, Bulldog, 10, Brindle, 50

This is c++ program, I got some errors on Xcode, Is there someone to help me on this problem?

Explanation / Answer

Just modify the line number 97 of your program

Your code

throw exception ("weight cannot be negative");

Just replace exception word with string., Like this

throw string ("weight cannot be negative");

That is enough to make your code works