| 
//**************************************
//     
// Name: Highscore
// Description:This is a simple highscor
//     e system with an explanation of how it w
//     orks. With minor editing you can impleme
//     nt it into your own program.
// By: Evan Knight
//
//This code is copyrighted and has// limited warranties.Please see http://
//     www.Planet-Source-Code.com/vb/scripts/Sh
//     owCode.asp?txtCodeId=7348&lngWId;=3//for details.//**************************************
//     
//Programed by Evan
//12/15/03
//This is a highscore program. It saves 
//     a seperate file on your computer to keep
//     
//the high scores saved. Can be implemen
//     ted into a program with some minor chang
//     es.
#include <iostream> //standard header
#include <fstream> //used for ifstream
using namespace std;
struct data //Data Abstraction, each variable holds this info.
    {char name[20]; //20 characters for a name
    int score;}; //score
    void score(data[],int,data); //pass array a, how many elements in a,array b into
    //score function
    int main()
        {data a[5],b; //initalize variable a[5] with data type data, and b with data
        ifstream ReadIn; //call the read in function(ifstream) and name(Readin) it.
        ReadIn.open("highscore.dat"); //open file highscore.dat
        if (ReadIn.fail()) //if highscore.dat doesn't exist
            {
            cout<<"File highscore.dat not found!\nCreating blank high score.\n";
            data temp[5]={{"Evan",0}, //creates temp function and initializes
                {"Evan",0}, //all elements with "Evan",0.
                    {"Evan",0},
                        {"Evan",0},
                            {"Evan",0}};
                            	ofstream fout("highscore.dat"); //fout writes to highscore.dat
                            	for (int i=0;i<5;i++) //loop
                            	fout<<temp[i].name<<" "<<temp[i].score<<endl; //write blank list to file
                            	fout.close(); //no longer need fout, so it's gone
                            for (int i=0;i<5;i++)
                            a[i]=temp[i]; //make a[5] equal to temp[5]
                        }
else //if highscore.dat does exist
                        for (int i=0;i<5;i++) 
                        ReadIn>>a[i].name>>a[i].score; //read highscore.dat into program
                        for (int i=0;i<5;i++)
                        cout<<i+1<<". "<<a[i].name<<" "<<a[i].score<<endl; //print highscore list
                        cout<<"Please enter name for new highscore: ";
                        cin>>b.name;
                        cout<<"Please enter score for highscore: ";
                        cin>>b.score;
                        score(a,5,b); //call score function, pass in values data a,int 5,data b.
                        for (int i=0;i<5;i++)//int 5=number of elements in array a
                        cout<<i+1<<". "<<a[i].name<<" "<<a[i].score<<endl; //print highscore list
                        	ofstream fout("highscore.dat"); //call the read out function(ofstream) and
                        	for (int i=0;i<5;i++)//name(fout) it.
                        	fout<<a[i].name<<""<<a[i].score<<endl; //read out highscore list into file
                        	fout.close(); //no need for fout anymore
                        return 0;//end program
                    }
 void score(data a[],int d,data b)
                        {
                        data temp; //need temp data for switching
                        if (b.score>a[4].score) //if b.score is bigger than a[4].score
                            {a[4]=b; //make the last score of the list equal to b(score just entered)
                            for (int i=d-1;i>0;i--) //now that the score is on the list, just check and sort
                            if (b.score>a[i-1].score)
                                {temp=a[i]; //switch - if the 5th spot is bigger than
                                a[i]=a[i-1]; //the 4th spot,switch. if 4th spot is bigger
                                a[i-1]=temp;} //than 3rd spot, switch. etc...
                            }
 }
 
 |