I'm writing a program that will eventually be a Tic-Tac-Toe game, but as for now, I just want to get the layout done. Right now, all it does is read the game board from a file, display it, and then ask you where you want to save it afterwards. I'm just having a couple problems though.

When reading in a file, the format is as follows. There is exactly one space in between each symbol. The "X" symbolizes that played 'X' has taken the spot, the "O" symbolizes that played 'O' has taken the spot. A period represents an empty space:

X O .
. . .
. X .

In the display function, the game board is then displayed to the screen with player positions X and O which were read from the file, but periods are now left as blank spaces.

Here is my code so far. What is wrong?!

#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;

void readFile(char ticTacBoard[][9]);
void writeFile(char ticTacBoard[][9]);
void display(char ticTacBoard[][9]);

/**********************************************************************
* Main: Basically a delegator. Gets others to do its' dirty work.
***********************************************************************/
int main()
{
   //Declare array
   char ticTacBoard[9][9];

   //Calling functions/pass array
   readFile(ticTacBoard);
   display(ticTacBoard);
   writeFile(ticTacBoard);

   return 0;
}


/**********************************************************************
* Displays the results to the screen.
***********************************************************************/
void display(char ticTacBoard[][9])
{
   cout << " " << ticTacBoard[0][0] << " | " << ticTacBoard[1][0] << " | " << ticTacBoard[2][0] << " " << endl
        << "---+---+---" << endl
        << " " << ticTacBoard[0][1] << " | " << ticTacBoard[1][1] << " | " << ticTacBoard[2][1] << " " << endl
        << "---+---+---" << endl
        << " " << ticTacBoard[0][2] << " | " << ticTacBoard[1][2] << " | " << ticTacBoard[2][2] << " " << endl;
}


/**********************************************************************
* Read a file into memory.
***********************************************************************/
void readFile(char ticTacBoard[][9])
{
   //Declare variable/array
   char sourceFile[256];

   //Declare file-input.
   ifstream fin;

   //Get filename from user
   cout << "Enter source filename: ";
   cin >> sourceFile;

   //Open file with error checking
   fin.open(sourceFile);
   if (fin.fail())
   {
      cout << "Input file opening failed.\n";
      exit(1);
   }

   //Read from file into array
   for (int i = 0; i <= 9; i++)
   {
      fin >> ticTacBoard[i];
   }

   //Close the file
   fin.close();
}


/**********************************************************************
* Write a file into memory.
***********************************************************************/
void writeFile(char ticTacBoard[][9])
{
   //Delcare file-output
   ofstream fout;
   char destinationFile[256];

   //Asking for user input
   cout << "Enter destination filename: ";
   cin >> destinationFile;

   //Open destination file & error checking
   fout.open(destinationFile);
   if (fout.fail())
   {
      cout << "Output file opening failed.\n";
      exit(1);
   }
   else
      cout << "File written";

   //Writes board to file
   for (int i = 0; i <= 9; i++)
   {
     fout << ticTacBoard[i-1] << " ";

   if (i % 3 == 0)
   {
       fout << endl;
   }

 }

   //Close file
   fout.close();
}

I'm writing a program that will eventually be a Tic-Tac-Toe game, but as for now, I just want to get the layout done. Right now, all it does is read the game board from a file, display it, and then ask you where you want to save it afterwards. I'm just having a couple problems though.

When reading in a file, the format is as follows. There is exactly one space in between each symbol. The "X" symbolizes that played 'X' has taken the spot, the "O" symbolizes that played 'O' has taken the spot. A period represents an empty space:

X O .
. . .
. X .

In the display function, the game board is then displayed to the screen with player positions X and O which were read from the file, but periods are now left as blank spaces.

Here is my code so far. What is wrong?!

#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;

void readFile(char ticTacBoard[][9]);
void writeFile(char ticTacBoard[][9]);
void display(char ticTacBoard[][9]);

/**********************************************************************
* Main: Basically a delegator. Gets others to do its' dirty work.
***********************************************************************/
int main()
{
   //Declare array
   char ticTacBoard[9][9];

   //Calling functions/pass array
   readFile(ticTacBoard);
   display(ticTacBoard);
   writeFile(ticTacBoard);

   return 0;
}


/**********************************************************************
* Displays the results to the screen.
***********************************************************************/
void display(char ticTacBoard[][9])
{
   cout << " " << ticTacBoard[0][0] << " | " << ticTacBoard[1][0] << " | " << ticTacBoard[2][0] << " " << endl
        << "---+---+---" << endl
        << " " << ticTacBoard[0][1] << " | " << ticTacBoard[1][1] << " | " << ticTacBoard[2][1] << " " << endl
        << "---+---+---" << endl
        << " " << ticTacBoard[0][2] << " | " << ticTacBoard[1][2] << " | " << ticTacBoard[2][2] << " " << endl;
}


/**********************************************************************
* Read a file into memory.
***********************************************************************/
void readFile(char ticTacBoard[][9])
{
   //Declare variable/array
   char sourceFile[256];

   //Declare file-input.
   ifstream fin;

   //Get filename from user
   cout << "Enter source filename: ";
   cin >> sourceFile;

   //Open file with error checking
   fin.open(sourceFile);
   if (fin.fail())
   {
      cout << "Input file opening failed.\n";
      exit(1);
   }

   //Read from file into array
   for (int i = 0; i <= 9; i++)
   {
      fin >> ticTacBoard[i];
   }

   //Close the file
   fin.close();
}


/**********************************************************************
* Write a file into memory.
***********************************************************************/
void writeFile(char ticTacBoard[][9])
{
   //Delcare file-output
   ofstream fout;
   char destinationFile[256];

   //Asking for user input
   cout << "Enter destination filename: ";
   cin >> destinationFile;

   //Open destination file & error checking
   fout.open(destinationFile);
   if (fout.fail())
   {
      cout << "Output file opening failed.\n";
      exit(1);
   }
   else
      cout << "File written";

   //Writes board to file
   for (int i = 0; i <= 9; i++)
   {
     fout << ticTacBoard[i-1] << " ";

   if (i % 3 == 0)
   {
       fout << endl;
   }

 }

   //Close file
   fout.close();
}

You need a few minor adjustments to your array. For one, it's too big. [9][9] has 81 spots... all you need is [3][3]. When you read into your array, you're reading into [0][1], [0][2], [0][3]... up to [0][9]. But then when you display, you're doing things like cout << array[1][2]. Which is 9+2 or the 11th spot.

What you need to do is read in this order [0][0] [0][1] [0][2] [1][0] [1][1].. etc.

Then output in the same manner.
-Greywolf

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.