You are not logged in.

#1 2007-10-13 16:47:58

gunnarbot
Member
From: Umeå, Sweden
Registered: 2007-09-06
Posts: 18
Website

C++ Program runs 7 times then gets weird

I'm fairly new to programming and I just started learning about file access..
This program reads a highscore-list from a file, prints it, asks for a new highscore entry (player and score), sorts it into the list and writes the number of highscore-entries and all the entries to a file in binary mode.
This program would be perfect for a really boring game, because if the game is really boring you just play it a few times and you would never notice the problem with it, because when you start this highscore-function after adding the 6th entry to the list, it prints some weird stuff..

This is the code.
http://pastebin.com/m5d66a62f
I would be really thankful for any help smile

Offline

#2 2007-10-13 19:49:32

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: C++ Program runs 7 times then gets weird

You have troubles with index :
1) the entries var in main function
2) i and j in the sort function

About sort, you should also check out the Bubble sort on wikipedia.
Note that in your specific case, you don't need a full sort, you only need to sort one variable at each step.

Finally, you should check out what happens when giving wrong / very long entries for Player or Score.


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#3 2007-10-16 10:19:24

gunnarbot
Member
From: Umeå, Sweden
Registered: 2007-09-06
Posts: 18
Website

Re: C++ Program runs 7 times then gets weird

Thanks alot for your help. smile Got everything working the way I want now

Offline

#4 2007-10-16 17:34:56

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: C++ Program runs 7 times then gets weird

gunnarbot wrote:

Thanks alot for your help. smile Got everything working the way I want now

Cool, I hesitated to send a little patch, but I thought you could probably figure it out yourself smile
But now, I'm curious, could you post your fixed version? smile


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

#5 2007-10-22 09:20:45

gunnarbot
Member
From: Umeå, Sweden
Registered: 2007-09-06
Posts: 18
Website

Re: C++ Program runs 7 times then gets weird

http://pastebin.com/m35c271fe
That's what I did before I started reading about ncurses smile
I don't know if there is any possibility to have safe input in normal C++. Any suggestions would be appreciated smile

Offline

#6 2007-10-22 18:08:56

shining
Pacman Developer
Registered: 2006-05-10
Posts: 2,043

Re: C++ Program runs 7 times then gets weird

It's a bit better than before, but it's not there yet smile

if (entries < 10) {
blabla
else if (entries == 10) {
blabla

That covers all normal cases, so the two others else are never run.
Because when your program is used correctly, you always have entries <= 10 .


pacman roulette : pacman -S $(pacman -Slq | LANG=C sort -R | head -n $((RANDOM % 10)))

Offline

Board footer

Powered by FluxBB