OT: problem with C++

Joined
Aug 15, 2007
Messages
316
I'm sorry this is off topic, but I really need some help with some coding. I'm in a first year university programing class working with C++ and for the most part I'm figuring things out okay, but on my most recent lab I'm running into some kind of error. The program is supposed to read in a game board of .'s and X's from a txt file, run it through a function that spawns or kills X's according to some rules, and print to another txt file. I would be very grateful if someone could look at my code and tell me what I've done wrong. (The code is supposed to be unfinished, I'm just testing the main function atm).

Spoiler :
Code:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;

const int MAX_ARRAY_SIZE=50;

// function prototypes
void PrintGen(char lifeBoard[][MAX_ARRAY_SIZE], ofstream outStream, int numRowsInBoard, int numColsInBoard, int generationNum);
void NextGen(char lifeBoard[][MAX_ARRAY_SIZE], int numRowsInBoard, int numColsInBoard);

int main()
{
	ifstream inStream;
	ofstream outStream;
	char lifeBoard[][MAX_ARRAY_SIZE]={'\0'}, inFile[100]="infile.txt", outFile[100]="outfile.txt";
	int numRowsInBoard=0, numColsInBoard=0, generationNum=0, rowIndex=0, colIndex=0;

	cout << "What is the name of the input file? \nfile name: ";
	cin >> inFile;

	inStream.open(inFile);
	if (inStream.fail())
	{
		cerr << "ERROR: Input file, " << inFile << " could not be opened.\n";
		return(1);
	}

	cout << endl << "What is the name of the output file? \nfile name: ";
	cin >> outFile; cout << endl;

	outStream.open(outFile);
	if(outStream.fail())
	{
		cerr << "ERROR: Output file, " << outFile << " could not be opened.\n";
		return(1);
	}

	inStream >> numRowsInBoard >> numColsInBoard >> generationNum;

	for (rowIndex=0; rowIndex<numRowsInBoard; rowIndex++)
	{
		//colIndex=0;

		for (colIndex=0; colIndex<numColsInBoard; colIndex++)
		{
			[B]inStream >> lifeBoard[rowIndex][colIndex];[/B] [I]This seems to be where the problem is. It seems to break at a random spot in the loop.[/I]
			
			/*if (lifeBoard[rowIndex][colIndex]!='X' && lifeBoard[rowIndex][colIndex]!='.')
			{
				cerr << "Input data for initial board is incorrect. \nLocation (" << rowIndex << ", " << colIndex << ") is invalid.\n";
				inStream.close();
				return(1);
			}

			if (lifeBoard[0][colIndex]=='X' || lifeBoard[numRowsInBoard-1][colIndex]=='X' || lifeBoard[rowIndex][0]=='X' || lifeBoard[rowIndex][numColsInBoard-1]=='X')
			{
				cerr << "All loacations on the edge of the board must be a '.' (a dot/period). \nLocation (" << rowIndex << ", " << colIndex << ") is invalid.\n";
				inStream.close();
				return(1);
			}
			cout << lifeBoard[rowIndex][colIndex];

			if ((colIndex%(numColsInBoard-1)==0) && colIndex>0)
			{
				cout << endl;
			}*/
		}
	}


	inStream.close();
	outStream.close();
	return(0);
}

void PrintGen (char lifeBoard[][MAX_ARRAY_SIZE], ofstream outStream, int numRowsInBoard, int numColsInBoard, int generationNum)
{
	int rowIndex=0, colIndex=0;

	for(rowIndex=0; rowIndex<numRowsInBoard; rowIndex++)
	{
		for(colIndex=0; colIndex<numColsInBoard; colIndex++)
		{
			outStream << lifeBoard[rowIndex][colIndex];

			if (generationNum == 0)
			{
				cout << lifeBoard[rowIndex][colIndex];
			}
		}
		
		cout << endl;
		outStream.close();
	}

	return;
}
 

Attachments

1) Join this group http://forums.civfanatics.com/group.php?groupid=276 and ask there

2) What problems are you having? Does it compile? Does it not work?

EDIT: Looks like you not checking array bounds for < 0. That will cause all sorts of problems.
EDIT2: Hmm, maybe not, I see you are checking the bounds in main.
 
The problem is you don't fully specify your array bounds.

char lifeBoard[][MAX_ARRAY_SIZE]

creates a pointer to an array (size MAX_ARRAY_SIZE) and doesn't allocate any space for the 2nd dimension of the array.

Change it to

char lifeBoard[MAX_ARRAY_SIZE][MAX_ARRAY_SIZE]

for now.

Prefer a std::vector of std::vectors though, or else allocate the array as a fixed size with operator new. Best bet is probably to make a class representing the game board though. If you don't know how to do dynamic memory allocation with new though, just stick with a fixed size array for now.
 
Thanks, I'll try that, but I don't think that that's the problem. The way I've declared the array is how the prof has instructed us to and I've done this in other programs and it's worked. As for the alternate ways you suggested, unfortunately I haven't learned any of that yet, not even about pointers. Even if I had time to learn it myself, the lab specifically says to use arrays. Whether or not they're the best way is irrelevant because the prof is looking for how I use arrays. Thank you though! :)
 
That's how you declare a 2D array as a parameter to a function not as a local declaration, unless you initialise all the members (compiler works out the correct size in that case).
 
Back
Top Bottom