1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
/Define a class Hammurabi with a member function that takes a parameter;
//Create a Hammurabi object and call its displayMessage function.
#include <iostream> //Header library for the input, output stream
#include <cstdlib> //Header library defines general purpose functions including random number generation
#include <time.h> //Header library allows the change of numbers per certain length of time
using namespace std;
//Hammurabi class definition
class Hammurabi
{
public:
//function that displays a message to the Hammurabi user
//Print out the introductory message
void displayMessage (int year, int starved, int immigrants, int population, int land, int harvest, int rats, int storage, int trade)
{
cout << "Hammurabi: I beg to report to you that in Year " << year << endl << endl;
cout << starved << " people starved;" << endl;
cout << immigrants << " immigrants came to the city" << endl;
cout << "The city population is " << population << endl;
cout << "The city now owns " << land << " acres" << endl;
cout << "You harvested " << harvest << " bushels per acre;" << endl;
cout << "Rats ate " << rats << " bushels;" << endl;
cout << "You now have " << storage << " bushels in storage;" << endl;
cout << "Land is trading at " << trade << " bushels per acre" << endl;
cout << endl;
}//end function displayMessage
};//end class Hammurabi
//function main begins program execution
int main()
{
//variables to store the values
int year = 0;
int starved = 0; //people who starved, population loss
const int immigrants = 5; //people who came to the city, population gain
int population = 100;
int land = 1000; //amount of land, acres owned by the city
const int harvest = 3; //amount of bushels harvested per acre planted
const int rats = 10; //amount of bushels destroyed by rats
int storage = 2500; //amount of bushels in storage
int trade = 15; //price land is trading, how many bushels per acre
while (year <=11 && population > 0) {
year = year + 1;
srand((unsigned)time(NULL));
//trade = 15 + (rand() % 5) + 1;
Hammurabi myHammurabi; //create a Hammurabi object named my Hammurabi
//call my Hammurabi displayMessage function
//and pass values as an argument
myHammurabi.displayMessage(year, starved, immigrants, population, land, harvest, rats, storage, trade);
int buy; //amount of acres to buy
int sell; //amount of acres to sell
int food; //amount of bushels to feed the population
int plant; //amount of acres to plant with bushels
cout << "How many acres of land do you want to buy? " << endl; //amount of bushels to to trade for land
cin >> buy;
land += buy; //assignment by sum and difference, (land = land + buy)
storage -= buy * trade;
cout << "How many acres of land do you want to sell? " << endl;
cin >> sell;
land -= sell;
storage += sell * trade;
cout << "How many bushels do you want to feed to the people? (each needs 20) " << endl;
cin >> food;
storage -= food;
cout << "How many acres do you want to plant with seed? (each acre takes one bushel) " << endl;
cin >> plant;
storage -= plant;
cout << endl;
population += immigrants;
storage -= rats;
storage = storage + (harvest * plant);
system("pause");
return 0;
}//end main
}
|