Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Need help using array to store score

Author
Message
clak
12
Years of Service
User Offline
Joined: 31st Jan 2012
Location:
Posted: 31st Jan 2012 22:05
This is my first post here and I'd like to thank anyone for reading this. I'm taking a class using DB and this forum and site in general is a great resource for tutorials.

Now to my problem. In the assignment I need to create a guessing game, which I've done after reading this thread which was a lot of help. So I've got a working game, but I need to be able to store a score in an array, and then I need to be able to save that array out to a file.

I've read a few threads already like this one and I think understand how to actually save the data to a file. My problem though is that I don't know how to actually get the score into an array to eventually save out to a file.

Say I've got something like:



If the player guesses right I need to be able to assign a score to the correct answer and then store it in an array.
This is what I'm having the most trouble with it seems. I could store the score in a simple variable, but I have to use an array for the assignment. Once I get the scores in an array I need to save them to a file and read it back to show all the scores.
Hodgey
15
Years of Service
User Offline
Joined: 10th Oct 2009
Location: Australia
Posted: 1st Feb 2012 09:36
In this case two arrays, one for the scores and one for the names (for now I'm assuming you are saving names as well) would be sufficient. Some may suggest using a User Defined Type but for now two parallel arrays will be fine.

When you run your program, you would read the current scores and names from the file in to each appropriate array. Then if the user's score is high enough, you would insert it into the array (as well as their name. For this task I'd recommend an insertion sort. Once that is done, you'd then write the values in the arrays back into the file.

The above simplified:
1. Read scores (and names) from the file (you can manually type in some 'default' values) and insert them into your array(s).
2. Let the game play and check to see if the user has achieved a highscore.
3. If they have then perform an insertion sort to insert the new values into the array(s).
4. Write the values back into the file. This may involve deleting the current one and creating a new file.

nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 1st Feb 2012 09:38
Is saving the array to hdd a prt of the assignment too. If so, look up the commands LOAD ARRAY and SAVE ARRAY. Here's a quick example:


clak
12
Years of Service
User Offline
Joined: 31st Jan 2012
Location:
Posted: 1st Feb 2012 20:30 Edited at: 2nd Feb 2012 18:03
Looking at the assignment again (the instructor is a bit confusing with different sets of requirements in different areas) we don't need to store high scores apparently, just the number of guesses a player makes.

This is what I'm going off of:

◦Select a Random Number
◦Ask user’s name
◦Loop
◦Ask the User to enter a number
◦Increment Counter
◦Is correct? Exit function
◦EndLoop
◦Write to a Score file the name and number of guesses
◦Print out all previous users and guesses

So yes, I guess I'll need to use a save/load array to store it. To assign a score I guess I could add something like the above within my if statement.

Something like:

nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 2nd Feb 2012 08:54
You're going to have to then make some decisions as to the outline of your project. Do you want to use a FUNCTION (I'm guessing from the reference to EXITFUNCTION, unless you mean EXIT as in EXIT loop) or do you want to make it all one module? So plan your structure first.

I dunno how they'll mark you as I'm self-taught but I would guess that the criteria will be neatness, understanding (ie comments to show your working), efficient code and compactness.

Without giving it away (that would defeat the purpose of your assignment), here's a hint to get you started (this is more or less the structure you should use at beginner level):


This next snippet is something to think about in your spare time. For your own safety, do not attempt to use a method like this until you have a complete understanding of program flow (you may lose marks too as I think most lecturers would be against this). Using GOTO is bad for beginners and this snippet is just to illustrate things from a different angle. In other words, cogitate:



BTW: The reason I printed out the number you have to guess is so you can play with the code more easily, not because I want to make the game too easy.

Well good luck and hopefully this helps.

PS: in the future, you should place any multiple lines of code in snippets, even if it's like the ones in your previous post or you may get MODded. You can edit posts with the Edit Post link (bottom left below your avatar).

clak
12
Years of Service
User Offline
Joined: 31st Jan 2012
Location:
Posted: 2nd Feb 2012 18:16
I've already got a working guessing game, as I originally said, that isn't the problem at all. I'm having trouble for one, saving scores in an array, and two, then saving those scores to a file.

For the sake of it I'll just post the entire code, hopefully that will make it clear where I'm stuck.



As it is, the only score stored in the array is the last score. I know why this is, it's because the variable can't store more than one score at a time and the array draws off of that, so I just end up with :
2
2
2
2
etc.

The problem is that I can't think of any other way to feed the array. I just started this, I've done some visual basic back years ago, but this is much different from that. We didn't do much with arrays in VB. I'm thankful for any help and apologize for forgetting to use the code tags.
nonZero
13
Years of Service
User Offline
Joined: 10th Jul 2011
Location: Dark Empire HQ, Otherworld, Silent Hill
Posted: 3rd Feb 2012 10:13
The reason you end up with the same value for each item in the array is because of this:
Quote: "

"

What you're doing here is taking "score" and assigning it to each array item and that's why they're the same. Here's a quick little something I boiled up. Copy it and paste it into your editor. Read through it and run it. It'll give you more or less all you need on arrays for this assignment:


Login to post a reply

Server time is: 2024-11-25 08:16:20
Your offset time is: 2024-11-25 08:16:20