High score question

This is a discussion on High score question within the Game Programming forums, part of the General Programming Boards category; here's the code I have now: Code: #include <iostream.h> #include <stdlib.h> #include <time.h> #include <conio.h> int rand(void); int main() { ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    8

    High score question

    here's the code I have now:
    Code:
    #include <iostream.h>
    #include <stdlib.h>
    #include <time.h> 
    #include <conio.h>
    int rand(void);
    
    int main()
    {
    	int tries;
    	tries = 0;
    	int num;
    	int inputnum;
    	srand(time(NULL));
    	num = rand()%101;
    	cout<<"Pick a number between 0-100"<<endl;
    	cin>>inputnum;
    	tries++;
    	bool loop = true;	
    	while( loop )	
    	{
    		if(inputnum == num)
    		{
    			cout<<"Your right! The number is "<<num<<". It took you "<<tries<<" tries."<<endl;
    			getch();
    			return 0;
    		}
    		while(inputnum > num)
    		{
    			cout<<"Try a little lower."<<endl;
    			cin>>inputnum;
    			tries++;
    		}
    		while(inputnum < num)
    		{
    			cout<<"That's too low."<<endl;
    			cin>>inputnum;
    			tries++;
    		}
    	}
    	return 0;
    }
    How would I write the score to a file, and check if it's a new high score or not(least tries). I know you use the function ofstream, but I don't quite understand how to use it. Anothe rconcern is how might you prevent someone from editing the high score?

  2. #2
    Frustrated Programmer :( phantom's Avatar
    Join Date
    Sep 2001
    Posts
    163
    Anothe rconcern is how might you prevent someone from editing the high score?
    There are whole books writtten on this! If blizzard can employ over 50 programmers on Diablo2 and the files still can get edited by hackers then one person really doesn't stand a chance. The only thing you can do is make it difficult to edit, hackers will only take the amount of time that they feel it is worth on a game. Your simple game they might give 10 minutes too and if they can't hack into it by then they will do something else, a game like Diablo2 they may have spent days on.
    My site to register for all my other websites!
    'Clifton Bazaar'

  3. #3
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    ofstream fout

    fout.open("HSCORE.DAT");
    fout<<score;
    fout.close;

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    8
    What might the starting .dat file look like?

  5. #5
    I'm Back
    Join Date
    Dec 2001
    Posts
    556
    let see... suppose the first time the program runs and the top score is say 100 then 100 will be written into the .dat file.
    so your starting .dat file says 100

    next time the game is played and the score comes to say 80 then you could read the score from the .dat file, compare it with the present score and using if-else write it into the .dat file or leave it.

    since we're not using ios::app only 1 top score remains in the file.

    for others to be unable to change the score in file put garbage text also and put the number in between. but be careful when you read the data. also you could code to display other characters instead of 100.

    hope this helps


  6. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    129
    If somebody was to edit a hs list of my game, I can't see why not if that makes him/her happy... not that it's something to be proud of!

    Anyway, I'd say that the ultimate breaker of most encryptions is de-icing the code and taking a look how it's done.
    kooma - t.h@iki.fi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C program using structs to calculate grades
    By TampaTrinDM88 in forum C Programming
    Replies: 4
    Last Post: 07-06-2009, 12:33 PM
  2. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  3. Functions Question
    By audinue in forum C Programming
    Replies: 2
    Last Post: 01-09-2009, 08:39 AM
  4. help on dropping score.
    By jchopki in forum C Programming
    Replies: 4
    Last Post: 09-21-2008, 06:54 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 02:57 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21