need help for saving high scores

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2006
Posts: 1
Reputation: coool_rahul is an unknown quantity at this point 
Solved Threads: 0
coool_rahul coool_rahul is offline Offline
Newbie Poster

need help for saving high scores

 
0
  #1
Nov 2nd, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 751
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: need help for saving high scores

 
1
  #2
Nov 2nd, 2006
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC