MechatronicJazz
Prince
- 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;
}