how do i get a function to write to a file for me?? im getting the error

error: invalid use of void expression

i want to save my tic tac game board to this file in order to save it.....

am i going the right way about it??

if(save_or_not == 'y'| save_or_not == 'Y')
                {
                    fstream file;
                    file.open("save1.txt",ios::in|ios::out);
                    file.write(DrawBoard());
                    file.close();
                 }

im gonna thank you guy in advance cos i had a prob before and my god you guys are generous to take time to help out...

thanks from the young beginner!!

Edited 6 Years Ago by __avd: Added [code] tags. Encase your code in: [code] and [/code] tags.

I'd change the single | to a double ||.

The ostream method write() takes two parameters and I find it difficult to see how the return type of DrawBoard(), whatever that is, can provide both parameters. If DrawBoard() uses cout you could write a second version using an ofstream (or an fstream in ios::out mode) or you could wrtie DrawBoard() generically to accept a reference to any valid output stream with the location of the output being determined by the stream passed to the function.

Edited 6 Years Ago by Lerner: n/a

Thanks for your reply and i got the syntax error on "||" but the drawboard function is what i would like to write to the file. it draws out the board in the text file so i can retrieve it and use it again.

You can't draw to a text file. I don't know what your game looks like, but if it contains a 2d array called a Board

char Board[5][5]; 

// write to a file
ofstream out("save1.bin", ios::binary);
out.write(Board, sizeof(Board));

// read from file
ifstream in("save1.bin", ios::binary);
out.read(Board, sizeof(Board));

You can't draw to a text file. I don't know what your game looks like, but if it contains a 2d array called a Board

char Board[5][5]; 

// write to a file
ofstream out("save1.bin", ios::binary);
out.write(Board, sizeof(Board));

// read from file
ifstream in("save1.bin", ios::binary);
out.read(Board, sizeof(Board));

Ok i've been at his for a few days now. il show my whole cpp... well without all the checks for win... can you help??

#include <iostream.h>
#include <stdlib.h>
#include <fstream.h>
#include "XO.hpp"

int player1_win=0;
int player1_lose=0;
int player2_lose=0;
int player2_win=0;
int total_games=0;
int draw=0;



void DrawBoard(void);

int Game::new_game()
{

    int m,n;
    char game_option='y';
    while(game_option=='Y'||game_option=='y')
    {
        for (m=0;m<3;m++)for (n=0;n<3;n++)*Board[m][n]= '\0';
        int i,j,sum=0;
        while ( sum < 10)
        {
            if (sum == 0) DrawBoard();
            cout<<"Player 1 is 'X': choose the row and column(to exit row=8 & col=9)"<<endl;
            cout<<"Row : ";
            cin>>i;
            cout<<"Column : ";
            cin>>j;
            if(i == 8 && j == 9)
            {
                char save_or_not;
                cout<<"Would you like to save the game?"<<endl;
                cin>>save_or_not;
                if(save_or_not == 'y'|| save_or_not == 'Y')
                {
                    void Save_Game(char *Board[3][3]);
                    break;
                }
                else
                {
                    break;
                }
            }
            else
            {
            for (;i>3 || i<1 || j>3 || j<1 ||('X'==*Board[i-1][j-1]||'O'==*Board[i-1][j-1]);)
            {
                cout<<"Sorry boy, but you gotta choose another place.\n";cout<<"row : ";cin>>i;cout<<"column : ";cin>>j;
            }
            *Board[i-1][j-1]='X';
            sum++;
            DrawBoard();

        //check if wins
            if (*Board[0][0]=='X' && *Board[0][0]==*Board[1][1] && *Board[1][1]==*Board[2][2])
            {
                player1_win++;
                player2_lose++;
                total_games++;
                cout<<"Player 1 wins"<<endl;
                break;
             }
            if (sum == 9)
            {
                cout<<"The game is over and no one wins, hahaha, your both low+!!"<<endl;
                total_games++;
                draw++;
                break;
            } //sum=9 because there are only 9 boxes in the game
            //player 2's turn

            cout<<"Player 2 is 'O': choose the row and column"<<endl;
            cout<<"Row : ";
            cin>>i;
            cout<<"Column : ";
            cin>>j;
            }



}
cout<<"\nWould you like to play again??? (Y - N)\n";
cin>>game_option;
}
      system("PAUSE");
      return 0;
}


void Game::Detail_Games(int player1_win,int player1_lose,int player2_lose,int player2_win)
{

    cout<<"-----------------------"<<endl;
    cout<<"|Player Results     | |"<<endl;
    cout<<"-----------------------"<<endl;
    cout<<"|Player 1 Wins:     |"<<player1_win<<"|"<<endl;
    cout<<"|Player 1 Lose(s):  |"<<player1_lose<<"|"<<endl;
    cout<<"|-------------------|-"<<"|"<<endl;
    cout<<"|Total Games:       |"<<total_games<<"|"<<endl;
    cout<<"|Total Draws:       |"<<draw<<"|"<<endl;
    cout<<"|-------------------|-"<<"|"<<endl;
    cout<<"|Player 2 Wins:     |"<<player2_win<<"|"<<endl;
    cout<<"|Player 2 Lose(s):  |"<<player2_lose<<"|"<<endl;
    cout<<"-----------------------"<<endl;
    cout<<""<<endl;
}

void Game::Load_Game()
{
}

void Game::DrawBoard(void)
{
//the play box
cout<<"\n\t      1   2   3\n"<<endl;
cout<<"\t   1  "<<*Board[0][0]<<" | "<<*Board[0][1]<<" | "<<*Board[0][2]<<endl;
cout<<"\t     ---|---|---\n";
cout<<"\t   2  "<<*Board[1][0]<<" | "<<*Board[1][1]<<" | "<<*Board[1][2]<<endl;
cout<<"\t     ---|---|---\n";
cout<<"\t   3  "<<*Board[2][0]<<" | "<<*Board[2][1]<<" | "<<*Board[2][2]<<"\n\n\n";
}

Game::Game()
{
	storageFile = new fstream("database.dat", fstream::in | fstream::out | fstream::binary);
}


void Game::Save_Game(char *Board[3][3])
{

ofstream out("save1.bin", ios::binary);

out.write(char *Board, sizeof(*Board[][]));

	cout<<"game saved";
}

To be honest im just making the program worse as i go along.. i am pretty much on the verge of quiting. any ideas how to save?

drawboard() is writing to standard input, and is returning void, so you can't print result to file. If you change function void Game::drawboard(void) to get reference to ostream, you will be able to print board on screen and to file.
PS: Why do you flush cout so much?

drawboard() is writing to standard input, and is returning void, so you can't print result to file. If you change function void Game::drawboard(void) to get reference to ostream, you will be able to print board on screen and to file.
PS: Why do you flush cout so much?

to be honest i just do it as good practice. When i get better at coding il try cut down but i wanna be better. any ideas on how to make that function reference ostream?

to be honest i just do it as good practice.

Well, its not "good practice" to use endl to often because all it does is slow down your program. I recall a couple unix operating systems that needed flushing often so that you could see the output on the screen quicker, but that was nearly 30 years ago -- operating systems have improved a tad since then. Instead of endl you can simply use '\n' to move the cursor to the next line.

This article has been dead for over six months. Start a new discussion instead.