How annoying it is to experiment this way:
int num_rabbits, num_foxes, num_grass;
double rabbit_birth_rate, rabbit_death_rate;
double grass_per_rabbit, rabbits_per_fox;
double fox_brith_rate, fox_death_rate;
int length_of_simulation;
void main(void)
{ cout << "How much grass? ";
cin >> num_grass;
cout << "How many rabbits? ";
cin >> num_rabbits;
cout << "How much grass does each rabbit want? ";
cin >> grass_per_rabbit;
cout << "Rabbit birth rate? ";
cin >> rabbit_birth_rate;
cout << "Rabbit death rate? ";
cin >> rabbit_death_rate;
cout << "How many foxes? ";
cin >> num_foxes;
cout << "How many rabbits does each fox want? ";
cin >> rabbits_per_fox;
cout << "Fox birth rate? ";
cin >> fox_birth_rate;
cout >> "Fox death rate? ";
cin >> fox_death_rate;
// now we can start the simulation.
This would be so much easier all round:
void main(void)
{ ifstream rfdata("conditions.txt");
rfdata >> num_grass;
rfdata >> num_rabbits;
rfdata >> grass_per_rabbit;
rfdata >> rabbit_birth_rate;
rfdata >> rabbit_death_rate;
rfdata >> num_foxes;
rfdata >> rabbits_per_fox;
rfdata >> fox_birth_rate;
rfdata >> fox_death_rate;
And this would be so much better to use:
void main(void)
{ ifstream rfdata("conditions.txt");
rfdata >> num_grass;
rfdata >> num_rabbits;
rfdata >> grass_per_rabbit;
rfdata >> rabbit_birth_rate;
rfdata >> rabbit_death_rate;
rfdata >> num_foxes;
rfdata >> rabbits_per_fox;
rfdata >> fox_birth_rate;
rfdata >> fox_death_rate;
while (true)
{ cout << "1: amount of grass = " << num_grass << "\n";
cout << "2: amount of rabbits = " << num_rabbits << "\n";
cout << "3: grass per rabbit = " << grass_per_rabbit << "\n";
// etc etc etc
cout << "9: fox death rate = " << fox_death_rate << "\n";
cout << "0: run simulation\n";
cout << "Your selection: ";
int select;
cin >> select;
if (select==0)
break;
else if (select==1)
{ cout << "How much grass? ";
cin >> num_grass; }
else if (select==1)
{ cout << "How many rabbits? ";
cin >> num_rabbits; }
// etc etc etc
else if (select==9)
{ cout << "Fox death rate? ";
cin >> fox_death_rate; }
else
cout << "Bad input, try again"; }
// start simulation here.
Global variables are bad.
What if I want to perform two simulations?
int num_rabbits1, num_foxes1, num_grass1;
double rabbit_birth_rate1, rabbit_death_rate1;
double grass_per_rabbit1, rabbits_per_fox1;
double fox_brith_rate1, fox_death_rate1;
int length_of_simulation1;
int num_rabbits2, num_foxes2, num_grass2;
double rabbit_birth_rate2, rabbit_death_rate2;
double grass_per_rabbit2, rabbits_per_fox2;
double fox_brith_rate2, fox_death_rate2;
int length_of_simulation2;
This would be so much better:
struct state
{ int num_rabbits, num_foxes, num_grass;
double rabbit_birth_rate, rabbit_death_rate;
double grass_per_rabbit, rabbits_per_fox;
double fox_brith_rate, fox_death_rate;
int length_of_simulation; };
state sim1, sim2, sim3;