Could someone please look at my code, Specifically the initSim Function

I am writing a program that will simulate a forest fire, I have moved many functions into one larger function called initSim where each should be called in order to get the required inputs from the 'user' and display the forest. When I run the program I do not get any of the questions for the user in the console, and thus the program does not display the desired output.

I do have a bunch of code commented out as it is not needed yet.

I have a screenshot of the output if that would help. But in the mean time here is my code:

/**************************************************************
** Chris Leonard ~ CpSc246 Spring 2012 ~ Forest Fire Sim **
***************************************************************
** Ask user for burn probablility, how fire will spread, **
** and start point. Burn Forest graphically, display **
** report, and allow reset of Forest. **
***************************************************************
** Adapted from www.shodor.org/interactivate/activities/Fire/ **
***************************************************************/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>
#include <time.h>
#include <stdio.h>
using namespace std;

const int MAX = 30;
const char TREE = '+';
const char BURNING = '#';
const char BURNEDOUT = '.';
char forest [MAX][MAX];


//bool isNeighborOnFire(forest, row, column);
void displayForest(char [MAX][MAX]);
void getInput(int& rowTree, int& colTree);
void getProbability(int& probFactor);
void initForest(char forest[MAX][MAX],int row, int column);
void initSim(char forest [MAX][MAX], int& probFactor);
void printReport();
void printRequired246Headings();
void updateForest(char forest [MAX][MAX]);
void wait(float seconds);

/**********************************************************
************************************************* M A I N *
**********************************************************/
1
2
3
4
5
6
7
8
9
10
11
12
13
int main()
{
	int probFactor;
	printRequired246Headings();
	initSim(forest, probFactor);
	
	//displayForest(forest); call it from burn simulation module and results module
	//isNeighborOnFire(forest, row, column)-called from somewhere else.
	
	cout <<"\n\n*--Normal Termination--*\n";

	return;
}

/**********************************************************/
/******************************************* initForest ***/
// Initialize Forest with "trees"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void initForest(char forest [MAX][MAX], int row, int column)
{
	for (int r = 0; r < MAX; r++) //Initialize row[0] and row[29]
	{
		for (int c = 0; c < MAX; c++) //Initialize column[0] and column[29]
		{
			if(r == 0 || r == (MAX - 1))
				forest[r][c]= ' ';
			else if(c == 0 || c == (MAX - 1))
				forest[r][c] = ' ';
			else if(r == row && c == column)
				forest[r][c] = BURNING; //Set start point of burning tree
			else
				forest[r][c] = TREE; //Initailizes remainder of array	
		}//end for column loop
	}//end for row loop
	return;
}	

/**********************************************************/
/************************************** displpayForest ***/
// Displays the Forest
1
2
3
4
5
6
7
8
9
void displayForest(char [MAX][MAX])
{
	for (int i = 0; i < MAX; i++)
	{
		cout << "\n";
		for (int j = 0; j < MAX; j++)
			cout << forest[i][j];
	}
}

/**********************************************************/
/********************************************* getInput ***/
//Get user input for row and column of tree to start burn
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
void getInput(int& rowTree, int& colTree)
{
	// Input from user for selection of row and column for start point
	cout << "This program will simulate a forest fire based on user input" << endl;
	cout << "\nWhat row is your tree in? Please enter an interger between 1 & 28: ";
	cin >> rowTree;
	cout << "\nWhat column is your tree in? Please enter an interger between 1 & 28: ";
	cin >> colTree;

	// Validation loop to check if row is within 1 and 28
	while (rowTree <1 || rowTree >MAX - 2)
	{
		cout << "\nYour tree is not in the forest. Try Again.";
		cin >> rowTree;
	}

	// Validation loop to check if column is within 1 and 28
	while (colTree <1 || colTree >MAX - 2)
	{
		cout << "\nYour tree is not in the forest. Try Again.";
		cin >> colTree;
	}
}

/**********************************************************/
/*************************************** getProbability ***/
//Get Probability from User to determine success of fire
1
2
3
4
5
6
7
8
9
10
11
void getProbability(int& probFactor)
{
	cout << "\nPlease enter a probability for the forest fire between 1 & 100: ";
	cin >> probFactor;

	// Validation loop to check if probability is between 1 and 100
	while (probFactor <1 || probFactor >100)
	{
		cout << "\nPlease re-enter a probability between 1 & 100: ";
		cin >> probFactor;
	}

/**********************************************************/
/************************************ isNeighborOnFire ***/
//Function to determmine is neighboring tree is on fire
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*
bool isNeighborOnFire(forest, row, column)
{
	int dir = 0; // Set direction to zero
	bool onFire = false;

	// Declare and initialize a Direction Array for the 8 locations around a tree
	int dirAry [8][2] = {{-1,-1}{-1,0}{-1,-1}{0,-1}{0,1}{1,-1}{1,0}{1,1}};

	// This while loop scans through the Direction Array to help determine
	// the probability of neighboring trees being on fire
	while (dir <8 && !onFire)
	{
		int r = row + dirAry[dir][0];
		int c = column + dirAry[dir][1];

		if (forest[r][c] == onFire)
		{
			onFire = true;
		else
			dir++;
		}
	}	
} */

/**********************************************************/
/************************************** voidWait ****/
1
2
3
4
5
6
7
8
9
//This function adds a wait time in between the forest update
//void wait(float seconds)
//{
//	Stall "seconds" sec by system clock
//	clock_t endwait;
//	endwait = clock () + seconds * CLOCKS_PER_SEC ;
//	while (clock() < endwait);
	
//} 

/**********************************************************/
/**************************************** updateForest *******/
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
//
//void updateForest(char forest [MAX][MAX], int probFactor)
//{
//	char tempForest [MAX][MAX];
//		for (int r = 0; r < MAX -2; r++) //Walk through row to update
//	{
//		for (int c = 0; c < MAX - 2; c++) //Walk through column to update
//		{
//			if(isNeighborOnFire(forest, row, column))
//			{
//				if ((rand()%100)+1 < probFactor)
//					forest [r][c] = BURNING;
//			}
//			else if (forest [r][c] == BURNING)
//			{
//				forest [r][c] = BURNEDOUT;
//			}
//			else
//			{
//				//Else case is tree burned out or empty cell
//			}
//
//		}//end for column loop
//	}//end for row loop
//} 

/**********************************************************/
/********************************************* initSim ****/
//
1
2
3
4
5
6
7
8
9
10
void initSim(char forest [MAX][MAX], int& probFactor)
{
	//srand(time(NULL));
	int rowTree;
	int colTree;
	getInput(rowTree, colTree);
	getProbability(probFactor);
	initForest(forest, rowTree, colTree);
	displayForest(forest);
}

/**********************************************************
***************************** printRequired246Headings ***/
//
1
2
3
4
5
6
void printRequired246Headings()
{
	cout<<"\n\nChris Leonard ~ CpSc246.01 ~ Spring 2012\n";
	cout<<"  Forest Fire"<<endl;
	cout<<" Adapted from www.shodor.org/interactivate/activities/Fire/\n\n\n";
}

Last edited on
Please, repost your code using code tags.
could you explain what you mean about code tags? Im sorry I am completely new to this forum and do not understand.

Thanks
I hope that helps. Please accept my apologies for not understanding at first.
In void initSim(); I assume you are trying to call initSim() function here. When calling a function, you do not specify its type.

Main function should be of type int, and should return 0;
Last edited on
I changed initSim in Main to initSim(); and changed Main to int main returning 0. I am now getting an "error c2660: 'initSim' : function does not take 0 arguments"

what should I put in the () for initSim in main?
The function initSim() requires a two-dimensional char array and an integer(probFactor). You need to pass those as arguments to the function initSim().
I have fixed that and now get a much better output. initSim(forest, probFactor);
Now in the initForest should be displaying in the forest the first tree on fire. I believe because I have int row = 0 and int column = 0 that is why the first tree is not appearing on fire given the user input of row and column.

What should I do to fix this? If I take away the = 0 on either row or column I get a uninitialized error.

Thank you.
When you ask the user for input, you store the user's input in int rowTree and int colTree, yet you pass int row and int column to function initForest(). Try passing int colTree and int rowTree instead.
Last edited on
Thank you very much Wisely Done. I really appreciate your wisdom and insight.

Here is what the initSim fn looks like now and it runs... anything else you think I should change?

1
2
3
4
5
6
7
8
9
10
void initSim(char forest [MAX][MAX], int& probFactor)
{
	//srand(time(NULL));
	int rowTree;
	int colTree;
	getInput(rowTree, colTree);
	getProbability(probFactor);
	initForest(forest, rowTree, colTree);
	displayForest(forest);
}
Looks good, start working on your commented out functions.
Do they look ok or do any of them need some help that I obviously have overlooked?
Topic archived. No new replies allowed.