Computer Science Canada

Help with Interger Overflow

Author:  ZeroPaladn [ Fri May 12, 2006 8:17 am ]
Post subject:  Help with Interger Overflow

Hi guys, been a long time since I've made a post here, but here it is.

I have a bunch of variables inside a record, most of which are ints and reals. One of them just so happens to be a max hp one. maxhp is defined by a formula...
code:
players(1).maxhp := round (players(1).sta * 3.5)

of course, this is a game where you can save your stats, so I have the stamina stat being read form the text file...
code:
var file : string := "char1.txt"
var fnum : int
open : fnum, file : open
read : fnum, players(1).sta : 2 %used so that your stats can never go past 100, because the program wont bother reading past that ^^
close : fnum

anyways, to the point, after reading from the file, and crunching the number to get maxhp, there is an Interger Overflow in "round". Can anyone help me out here?

Author:  codemage [ Fri May 12, 2006 8:41 am ]
Post subject: 

Have you "put" out the value you're trying to round (before rounding) to make sure it's a valid int?

Author:  ZeroPaladn [ Fri May 12, 2006 11:43 am ]
Post subject: 

this problem only happens when i try to get players (1).sta from a text file. This doesn't happen when i declare the variable inside the program itself. I will post the source code i have now, as it may make it much more clear.

NOTE
The text file should look as follows...
code:
25
25
25
25


Here is the source code.

code:
View.Set ("offscreenonly,graphics:400;500,nobuttonbar,position:center;center")

type player :
    record
        x : real
        y : real
        spd : int
        sta : int
        str : int
        dex : int
        hp : real
        maxhp : int
        gp : real
        maxgp : int
        gotball : boolean
    end record
var players : array 1 .. 2 of player
var ballx, bally : array 1 .. 3 of int
var chars : array char of boolean

proc bg
    Draw.FillBox (0, 0, maxx, maxy, yellow)
    Draw.ThickLine (2, 2, maxx - 2, 2, 4, black)
    Draw.ThickLine (2, 2, 2, maxy - 2, 4, black)
    Draw.ThickLine (maxx - 2, maxy - 2, 2, maxy - 2, 4, black)
    Draw.ThickLine (maxx - 2, maxy - 2, maxx - 2, 2, 4, black)
    Draw.FillOval (maxx div 2, maxy div 2, 50, 50, brightred)
    Draw.FillOval (maxx div 2, maxy div 2, 45, 45, yellow)
    Draw.ThickLine (0, maxy div 2, maxx - 2, maxy div 2, 4, black)
end bg

proc pcontrol
    Input.KeyDown (chars)
    if chars (KEY_UP_ARROW) then
        players (1).y += players (1).spd / 10
    elsif chars (KEY_DOWN_ARROW) then
        players (1).y -= players (1).spd / 10
    end if
    if chars (KEY_LEFT_ARROW) then
        players (1).x -= players (1).spd / 10
    elsif chars (KEY_RIGHT_ARROW) then
        players (1).x += players (1).spd / 10
    end if
    const hpdif := round (((players (1).maxhp - players (1).hp) / players (1).maxhp) * 60)
    const gpdif := round (((players (1).maxgp - players (1).gp) / players (1).maxgp) * 60)
    Draw.FillOval (round (players (1).x), round (players (1).y), 15, 15, 43)
    Draw.ThickLine (round (players (1).x - 30), round (players (1).y + 30), round (players (1).x + 30 - hpdif), round (players (1).y + 30), 2, brightred)
    Draw.ThickLine (round (players (1).x - 30), round (players (1).y + 25), round (players (1).x + 30 - gpdif), round (players (1).y + 25), 2, brightblue)
end pcontrol

proc readstats
    var file : string := "dodgeball/saves/char1.txt"
    var fnum : int
    open : fnum, file, read
    read : fnum, players (1).str : 2
    read : fnum, players (1).sta : 2
    read : fnum, players (1).spd : 2
    read : fnum, players (1).dex : 2
    close : fnum
end readstats

readstats

players (1).x := 50
players (1).y := 50
players (1).maxhp := round (players (1).sta * 3.5) %This is where the Interger Overflow happens
players (1).hp := players (1).maxhp
players (1).maxgp := round (players (1).dex / 2)
players (1).gp := players (1).maxgp
loop
    cls
    bg
    players (1).spd := 15
    pcontrol
    View.Update
    delay (10)
end loop


Thanks for the help guys.

Author:  do_pete [ Fri May 12, 2006 11:49 am ]
Post subject: 

No, if it wasn't an integer it would say "Argument is wrong type". If it says "Overflow in Integer expression" then the integer is greater than maxint. For example:
code:
put maxint + 1

will give you this error

Author:  Delos [ Fri May 12, 2006 11:57 am ]
Post subject: 

For now go with codemage's suggestion and put the values of the player(1). record. In other words, dump the contents at the end of readstats. Post up what you get...might shed some light on this.

Author:  ZeroPaladn [ Fri May 12, 2006 12:01 pm ]
Post subject: 

hm... so your saying that my program is giving the variable players (1).maxhp a number higher than maxint... impossible. It isnt going higher than 100!

Author:  ZeroPaladn [ Fri May 12, 2006 12:06 pm ]
Post subject: 

disturbing...
168367746 for str
168367746 for sta
168367746 for spd
-2147470030 for dex

Author:  Cervantes [ Fri May 12, 2006 4:00 pm ]
Post subject: 

ZeroPaladn wrote:
disturbing...
168367746 for str
168367746 for sta
168367746 for spd
-2147470030 for dex

That character is strong like bull. Agile like fence post.

Try breaking your code up into chunks. I haven't looked closely at the code, but my first suspicion is there's something wacky with the file. Try eliminating (copy and paste all the code into a new file first) the excess code and just see what happens with reading the data in then outputting it.

Are you sure you need that : 2 in the read lines?
code:
read : fnum, players (1).str : 2

Are you sure you should be using read as opposed to get? ie. Did you manually input the data, or did you use Turing to input it with write?

Author:  NikG [ Fri May 12, 2006 5:58 pm ]
Post subject: 

code:
proc readstats
    var file : string := "dodgeball/saves/char1.txt"
    var fnum : int
    open : fnum, file, get
    get : fnum, players (1).str
    get : fnum, players (1).sta
    get : fnum, players (1).spd
    get : fnum, players (1).dex
    close : fnum
end readstats

This seems to work... so the problem is definately with the read.

Author:  MysticVegeta [ Sat May 13, 2006 8:11 pm ]
Post subject: 

Turing Reference wrote:
The read statement inputs each of the readItems from the specified file. These items are input directly using the binary format that they have on the file. In other words, the items are not in source (ASCII or EBCDIC) format. In the common case, these items have been output to the file using the write statement.
By contrast, the get and put statements use source format, which a person can read using an ordinary text editor.


Well as you can see until you write to the text file using the write command, you wont be able to read it with the read command. so just use the flexible get and put like NikG did

Author:  ZeroPaladn [ Sat May 13, 2006 9:09 pm ]
Post subject: 

Thanks guys, ill fix that up when i get to school on monday, and ill post the results.

Author:  ZeroPaladn [ Mon May 15, 2006 11:35 am ]
Post subject: 

Thanks guys, It worked!


: