hamurabi Loop

I had the code and then accidentally copied over my projects, so have had to start again, and I forgot how I got it to loop. This code will loop when it is all in main. but when I used messageDisplay function outside of main, i cannot get it to loop, have tried the for and while loop in several different places in the code, just cannot get it to work. cannot see where I am going wrong.

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
}
closed account (NyqLy60M)
1
2
system("pause");
return 0;


That code is inside of your while() loop.
so how do i change it so that messageDisplay will loop for 10 years
closed account (NyqLy60M)
Change your while() condition to while (year <=9 && population > 0) {}, that way it'll only be called 10 times. Or, if you want there to be 12 years (like you have set up now, 0-11) and that function only called 10 times, simply put a condition above it, like this:

1
2
3
4
if(year >= 2)
{
    myHammurabi.displayMessage(year, starved, immigrants, population, land, harvest, rats, storage, trade);
}


You also might want to move year = year + 1 (year++) to the end of your while loop... it doesn't make much sense if the year ends before you buy the year's supply.
Topic archived. No new replies allowed.