glogic_1 a écrit :
> Hey all
>   
Hi, I am tired so, I excuse if I am a bit "hard" ;)
I have some questions about your code.

> Im having a problem writing to text files. I am able to read from 
> them no problem but just cant write to it.
>
> struct sScore {
>         char cName[40];
>   
why did not you use a std::string ?

>         int score;
>   
Since, you prefix your struct (sScore) and the cName, why did not you 
prefix score with i (iScore) ?
> }TopTen[SCORES];
>
>
>   

a std::vector could have been a good choice.

> fstream OutFile;
>   

I would have preffered an ifstream to read and a ofstream to write 
(inside a separate function)
> OutFile.open("HighScore.txt",ios::in);
> if (OutFile){
>      while (!OutFile.eof())
>      {
>             OutFile >> TopTen[iNumber].cName;
>             OutFile >> TopTen[iNumber].score;
>             iNumber++;
>       }
> }
>   
Usually, you did not check a read error on end of file.
while( OutFile >> TopTen[iNumber].cName >> TopTen[iNumber].score )
    ++iNumber;

but probably an operator << on sScore could have been welcome.
and take care of "space" in your name entries.
>  
>
> The above code works fine for taking in player name and score no 
> prob but the below code, when im sending an amended topten back out 
> to the txt, just clears the txt file completely
>
> OutFile.open("HighScore.txt",ios::out);
>   

OutFile.close() ?
> if (OutFile)
> {
>       for (int i = 0; i < (iNumNames+1);i++)
>       {
>   
what is iNumNames ?
Your loop seems strange, if iNumNames is 2, I expect to have 2 index in 
TopTen so i =0, 1.
This is not the case here, i = 0, 1, 2
>        OutFile << TopTen[i].cName << " " << TopTen[i].score << endl;
>                          
>       }
> }
> OutFile.close();
>
> Now any help would be appreciated as im only starting out on C++ and 
> am completely lost and confusd at this moment in time
> cheers
> g
>
>
>   
Regards,
David

Reply via email to