|
From: Michal M. <mi...@mo...> - 2008-02-18 23:55:52
|
Shawn napsal(a):
> Hi everyone!,
>
> I have this code to overwrite the high score in the highscore.txt file
> in a RPG game that I'm working on for class. I keep getting a 'outfile
> undeclared' fist use this function when it gets to the bolded open file
> for output part. I included #include<fstream> at the beginning. Any
> idea's??
if (playerScore > highScore)
ofstream outfile;
is equivalent to
if (playerScore > highScore)
{
ofstream outfile;
}
so there is no outfile variable after the if statement
> Also, I tried putting curley brakets around after the if statement since
> it's a compound statement block, but then I get this error from dev c++
>
> empty character constant
you did something wrong, this is correct solution:
if (playerScore > highScore)
{
//declare file pointer for input
ofstream outfile;
//Open file for output
outfile.open("highscore.txt", ios::out);
//write data
outfile<<playerName<<playerScore<<endl;
//close file
outfile.close();
}
|