this is a c++ program for the simulation of gillians island i did. im a newbie in programming looking to improve on my skills. Can some assist me on how to break this code into functions. please dont rewrite the code i need just a sampler from code i can work from.

#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])

long numRhinos = 0;
long rhinosEaten = 0;
long numPlants = 0;
long maxPlants = 0;
long plantsEaten = 0;
long numLions = 0;
long numHectares = 0;
int anotherYear = 1;
int year = 1;

// Have the user input the number hectares, plants, rhinos and lions.
cout << "Enter the number of hectares on Gilligan's Island: "<< endl;
cin >> numHectares;

cout <<"Enter the number of plants on Gilligan's Island: " << endl;
cin >> numPlants;
cout <<"Enter the number of rhinos on Gilligan's Island: " <<endl;
cin >> numRhinos ;

cout <<"Enter the number of lions on Gilligan's Island: " << endl;
cin>> numLions;

// Create a loop where the user decides if they want to proceed

cout <<"\nThis is Year \n" <<year<<endl;

cout <<"Predation is occurring....\n" << endl;

// check that the land can suppor the number of plants
maxPlants = numHectares*100000;
if(numPlants > maxPlants){
numPlants = maxPlants;

cout <<"The number of plants is: \n" << numPlants <<endl;

// calculate the number of plants eaten by rhinos.
plantsEaten = numRhinos*5000;
cout <<"The Number of plants require by the rhinos to feed is:\n" <<plantsEaten <<endl;
if(plantsEaten > numPlants) {
cout <<"\nThe Ecology Crashed." <<endl;
else {
numPlants -= plantsEaten;
cout <<"The number of plants is: \n" << numPlants <<endl;

// calculate the number of rhinos eaten by lions
rhinosEaten = numLions*50;
cout <<"The lions will eat %d rhinos.\n" <<rhinosEaten <<endl;
if(rhinosEaten > numRhinos) {
cout <<"\nThe Ecology Crashed." <<endl;
else {
numRhinos -= rhinosEaten;

cout <<"The number of rhinos is:.\n" << numRhinos <<endl;
// Start reproduction of plants, rhinos and lions

cout <<"Reproduction is occurring......\n" << endl;

numPlants *= 50;
cout <<"The number of plants is:\n" << numPlants <<endl;

if(numRhinos >= 2) {
numRhinos *=6;
cout <<"The number of rhinos is:.\n" << numRhinos << endl;

if(numLions >= 2) {
numLions *=2;
cout <<"The number of lions is:.\n" << numLions <<endl;


cout <<"\nDo you want the simulation to continue (Enter 1 for yes and 0 for no): " <<endl;
cin >>anotherYear;

} // end of while loop

cout <<"\nThis simulation has ended.\n" << endl;


8 Years
Discussion Span
Last Post by Knoxx

Look at the code you have, divide it into chunks that make logical sense to be together. At a minimum, the input section and each of the calculations should be individual functions. You might have one function that groups all the calculations into a single call from the main( ).

Also, please use code tags to make your code more readable here.


your code goes here


This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.