944,129 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3847
  • C++ RSS
Nov 2nd, 2006
0

need help for saving high scores

Expand Post »
have prepared a code in c++ to save high scores and it is working well when i compile it in turbo c++and run the code .

but when i exit from the turbo c++ , the file to which i saved the high scores get corrupted and so when i again takes the turbo c++ , the files which i have written to the file are all missing . please help me . here is the code

  1. #include<iostream.h>
  2. #include<fstream.h>
  3. #include<string.h>
  4. #include<conio.h>
  5.  
  6. struct highscore {
  7. char name[50];
  8. int score;
  9. };
  10.  
  11. main()
  12. {
  13. int i,j,tempscore,tempno;
  14. char empty[]={"empty"};
  15. ifstream fin("highscore.txt");
  16. ofstream fout("highscore.txt");
  17. highscore h[11];
  18. j=0;
  19. fin.seekg(0);
  20. while (fin) {
  21. fin.read((char*)&h[j],sizeof(highscore));
  22. j++;
  23. }
  24. if (j==0) {
  25. for (i=0;i<10;i++) {
  26. strcpy(h[i].name,empty);
  27. h[i].score=0;
  28. }
  29. }
  30. fin.close();
  31. cout<<"enter the name\n";
  32. cin.getline(h[10].name,50);
  33. cout<<"enter the score\n";
  34. cin>>h[10].score;
  35. tempscore=0;
  36. for (i=0;i<10;i++) {
  37. tempscore=0;
  38. for (j=i;j<11;j++) {
  39. if (h[j].score>=tempscore) {
  40. tempscore=h[j].score;
  41. tempno=j;
  42. }
  43. }
  44. char name[50];
  45. int p;
  46. p=h[i].score;
  47. h[i].score=h[tempno].score;
  48. h[tempno].score=p;
  49. strcpy(name,h[i].name);
  50. strcpy(h[i].name,h[tempno].name);
  51. strcpy(h[tempno].name,name);
  52.  
  53. }
  54. fout.seekp(0);
  55. for (i=0;i<10;i++)
  56. {
  57. fout.write((char*)&h[i],sizeof(highscore));
  58. cout<<h[i].name<<"\t\t\t\t"<<h[i].score<<"\n";
  59. }
  60. fout.close();
  61. getch();
  62. }
Last edited by ~s.o.s~; Nov 2nd, 2006 at 1:38 pm.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
coool_rahul is offline Offline
1 posts
since Nov 2006
Nov 2nd, 2006
1

Re: need help for saving high scores

Opening the same file for input and output at the same time is not a good idea.

open for read
read it
close it
do stuff
open for write
write it
close it.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Initializing Multidimensional Arrays plz help!
Next Thread in C++ Forum Timeline: Adding a part to code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC