473,545 Members | 1,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

need help for saving high scores

1 New Member
i 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

#include<iostre am.h>
#include<fstrea m.h>
#include<string .h>
#include<conio. h>

struct highscore {
char name[50];
int score;
};

main()
{
int i,j,tempscore,t empno;
char empty[]={"empty"};
ifstream fin("highscore. txt");
ofstream fout("highscore .txt");
highscore h[11];
j=0;
fin.seekg(0);
while (fin) {
fin.read((char* )&h[j],sizeof(highsco re));
j++;
}
if (j==0) {
for (i=0;i<10;i++) {
strcpy(h[i].name,empty);
h[i].score=0;
}
}
fin.close();
cout<<"enter the name\n";
cin.getline(h[10].name,50);
cout<<"enter the score\n";
cin>>h[10].score;
tempscore=0;
for (i=0;i<10;i++) {
tempscore=0;
for (j=i;j<11;j++) {
if (h[j].score>=tempsco re) {
tempscore=h[j].score;
tempno=j;
}
}
char name[50];
int p;
p=h[i].score;
h[i].score=h[tempno].score;
h[tempno].score=p;
strcpy(name,h[i].name);
strcpy(h[i].name,h[tempno].name);
strcpy(h[tempno].name,name);

}
fout.seekp(0);
for (i=0;i<10;i++)
{
fout.write((cha r*)&h[i],sizeof(highsco re));
cout<<h[i].name<<"\t\t\t\ t"<<h[i].score<<"\n";
}
fout.close();
getch();
}
Nov 1 '06 #1
0 2220

Sign in to post your reply or Sign up for a free account.

Similar topics

3
3989
by: Irene | last post by:
Hi all, I have set up a simple VB program (and later on an ASP interface) to manage an Athletics database. I'm using Access 2000. To simplify, I have the Athlets, the Competitions and the Scores tables. When I want to list of the best scores/ranking, I just do:
19
4072
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate the code that implements managing unbound controls on forms given the superior performance of unbound controls in a client/server environment. I can...
4
1993
by: jim | last post by:
So I've had my knuckles rapped by a pro who can offer only vauge advice. I need to reduce the size of one of my tables. Here is the scene: I'm trying to track lots of test scores for individual middle school students. I have a student info table using a student ID number as a primary key. The columns in this table are:
2
2755
by: Anthony | last post by:
I have a team website that has several games for entertainment. One of the games is a javascript version of yahtzee. Can someone suggest a way, using javascript, to record a high score for the game instead of a high score for a particular user, session, or computer. Perhaps someone already has code written to do this. Any help would be...
4
1617
by: jocknerd | last post by:
About 10 years ago, I wrote a C app that would read scores from football games and calculate rankings based on the outcome of the games. In fact, I still use this app. You can view my rankings at http://members.cox.net/jocknerd/football. A couple of years ago, I got interested in Python and decided to rewrite my app in Python. I got it...
4
5406
by: kyle christian | last post by:
I am trying to save the high scores of a game I made. Now Im stumped on trying to search through the file. Should I use a string, array, or one of the STL containers to manipulate the information read from the file? I have tried the stringname.find("Name"); This only returns the first occurence, and doesn't look for other occurences.
8
3969
by: toefraz | last post by:
Hi, guys. I'm writing a little game, and I want to include a high scores list. I've got everything coded except for the sorting part. I want to use the std::sort() function, but I'm not quite sure how I could get it to work with the data in my vector. Here is a sample high score file in which each line is inputted into a vector element. I want...
8
15249
by: d24706 | last post by:
Hello, I hope someone can help me. I had to do the following assignment( i have most of it done just cant finish it off, question and source code below). Question? Write a Java program that asks the user to input the scores, as a percentage( e.g 87.4), of 10 students. The scores entered must be stored ina n array. The programme must...
1
1411
by: Punkis | last post by:
Hello all. I am having some questions about a subject. I am running a small social community using SocialEngine and i want to include phpbb3 with an arcade mod or any arcade board that support high scores. My problem is that i do not want my users to re-register into the board. So if I manage to encode the password during login, into the...
0
7459
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7803
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7411
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7749
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
3444
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3439
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1871
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1012
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.