3 Replies - 212 Views - Last Post: 02 December 2017 - 03:52 PM Rate Topic: -----

#1 coolandon13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 17

Tic-Tac-Toe fout issues

Posted 02 December 2017 - 02:27 PM

Hey, I am trying to just do a layout for a tic tac toe board right now that reads it from a file and then asks for a destination to save the file. Everything is working until it has to save the text under a different file name. Then it is skipping all the spaces. I know it doesn't read white spaces except for using get line. as was as I know that is the only way to fix it, but I am not able to do that without losing all the information from the original file, because it only reads 3 by 3. I hope this makes sense. Here is my code. Again, my biggest issue is the code just not printing all the text within the ofstream.
#include <iostream>
#include <fstream>
using namespace std;

/**********************************************************************
*READ FILE
*
 ***********************************************************************/
void readFile(const char fileName[256])
{
   char grid[3][3] = {}; // 3 rows and 3 columns
   int cRow = 0;
   int cColumn = 0;
   ifstream fin;
   fin.open(fileName);
   for (cRow = 0; cRow < 3; cRow++)
   {
      for (cColumn = 0; cColumn < 3; cColumn++)
      {
         fin >> grid[cRow][cColumn];
         if (grid[cRow][cColumn] == '.')
         {
            grid[cRow][cColumn] = ' ';
         }
         else
         {
            grid[cRow][cColumn];
         }
         cout << " " << grid[cRow][cColumn];
         if (cColumn != 2)
         {
            cout << " |";
         }
         else
         {
            cout << " ";
         }
         if (cColumn == 2)
         {
         cout << endl;
         if (cRow != 2)
         {
            cout << "---+---+---\n";
         }
         }
      }
   }
   fin.close();

   char dFileName[256] = {};
   cout << "Enter destination filename: ";
   cin >> dFileName;

   ofstream fout(dFileName);
   if (fout.fail())
   {
      cout << "Error reading file " << dFileName;
      return;
   }
   else
   {
      cout << "File written" << endl;
   }
   for (cRow = 0; cRow < 3; cRow++)
   {
      for (cColumn = 0; cColumn < 3; cColumn++)

      {
         fout << (grid[cRow][cColumn]);
      }
   }
   fout.close();
}

/**********************************************************************
*GET FILE
*
 ***********************************************************************/
void getFile(char fileName[256])
{
   cout << "Enter source filename: ";
   cin >> fileName;
   readFile(fileName);
}

/**********************************************************************
*
*
 ***********************************************************************/
int main()
{
   char fileName[256];
   getFile(fileName);

   return 0;
}

This post has been edited by snoopy11: 02 December 2017 - 10:12 PM
Reason for edit:: fixed botched code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Tic-Tac-Toe fout issues

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2030
  • View blog
  • Posts: 5,430
  • Joined: 27-December 05

Re: Tic-Tac-Toe fout issues

Posted 02 December 2017 - 03:37 PM

No, it doesn't make sense. It's reading 9 characters, and it's writing 9 characters. If that's not what you want you'll have to state your objective more clearly.
Was This Post Helpful? 0
  • +
  • -

#3 coolandon13  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 02-December 17

Re: Tic-Tac-Toe fout issues

Posted 02 December 2017 - 03:44 PM

I realize it didn't make sense. and I was actually doing it right. I just had an extra line of code that was throwing everything off. Once I fixed that everything was working just fine. Thanks
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2030
  • View blog
  • Posts: 5,430
  • Joined: 27-December 05

Re: Tic-Tac-Toe fout issues

Posted 02 December 2017 - 03:52 PM

I would suggest that you reorganize this a bit. It makes little sense for a function called "getFile" to do everything including writing the output file. getFile should only do the file input and the output should be done in another function. And to accomplish that sensibly (along with the next step which is presumably to play the game, it would be better to create the grid array in main and pass it to the other functions as an argument.

And by the way, this block of code
            else
            {
                grid[cRow][cColumn]; // This statement has no effect.  In fact, the entire else block is unnecessary.
            }

has no effect and serves no purpose (and should be eliminated).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1