Recommended Posts

I recently got bored and wanted to create a program that will calculate GPA for a Semester. I have no real programming experience and I know my work is probably pretty sloppy, but i was wondering if someone could download what I have so far and tell me why im getting this error. It will calculate your GPA correctly the 1st time, but anything after that, it will read, "Your GPA is: -1.#IND".

incase you cant figure out how to use it, you put the amount of credits the class was worth on the left side and the grade you got (in GPA format, ex. A=4 B=3 C=2 etc.) in the class on the right, then hit calculate.

Share on other sites

Are you dividing by zero ?

Live for the present,
Dream of the future,
Learn from the past. Share on other sites

Are you dividing by zero ?

No, open then .au3 i have attached and see for yourself.

Share on other sites

No, open then .au3 i have attached and see for yourself.

I have opened it and scanned it, but stopped trying to understand it all when I saw the Call("Check") in ASK and Call("ASK") in Check. At the bottom you are doing a division, so what are the values of these 2 variables?

Jos

Live for the present,
Dream of the future,
Learn from the past. Share on other sites

ok, the \$cgt/\$cct im assuming is the variables your talking about. \$cct is the total amount of credits. the boxes on the left are named \$c1c, \$c2c, \$c3c etc.(c1c stands for Class 1 Credit) basicaly \$cct adds them all up. and \$cgt is the amount of credits multiplied by the grade you got.

so say you put "4" in box \$c1c and "4" in box \$c1g, that means you got an A in a 4 credit class (1st class), then you put "3" in box \$c2c and "4" in \$c2g, that means you got an A in a 3 credit class(2nd class). then you put "3" in \$c3c and "2" in \$c3g, that means you got a C in a 3 credit class (3rd class). cct will add up 4 + 3 + 3, and \$cgt will add (4*4) + (3*4) + (3*2)

and then your \$GPA which is the last variable, is equal to \$cgt divided by \$cct.

i hope this helps

PS. The call("ask") inside the call("check") will only be used if the person input a number that wasnt between 2 and 7.

Share on other sites

What Jos is getting at is that Call() is an old function, there is no use for it. Just call the function like so, myFunc()

Share on other sites

Im sorry i dont know what you mean by, "just use myFunc()"

but if u notice, in the example i put in the last post, thats 3 classes, A in 4 credit class, A in 3 credit class, and C in 3 credit class.

You can put these numbers into my program and it will work fine the 1st time. it says "Your GPA is: 3.4" which is correct, but if you click ok and try to calculate another GPA .. it will give the dreaded -1.#IND

Edited by ragnarok775

Share on other sites

What Jos is getting at is that Call() is an old function, there is no use for it. Just call the function like so, myFunc()

Thats not really what I am getting at. Call() is not an old function but should be used when the called func is a variable. My point is that this is a potential Stack overflow. In general a "called" function should never be able to call itself unless you do an intentional recursive operation like traversing through a directory structure.

@ragnarok775:

Put in some Debug statements (MSGBOX or ConsoleWrite) to check the exact values of the variables \$cgt and \$cct.

When they contain a Literal the numeric value will always be 0. You could do a Number() function to ensure is a proper number you are calculating with.

Jos

Live for the present,
Dream of the future,
Learn from the past. Share on other sites

Lets say you have a function called Check() instead of using Call("Check") use Check().

Try resetting the variables after the calculation.

Share on other sites

@Jos

ok i added the line MsgBox(0, "TEST", \$cgt & " / " & \$cct) right before the \$GPA = \$cgt / \$cct

the first time around it will work correctly, i put in the nnumbers fromt he example i gave and it ouputs, 34 / 10. but if i try to calculate again, no matter what i put int he boxes, it outputs 0 / 0

@JamesB

I had a function in their before that at the end of the func Calculate() it would go thru \$c1c - \$c7c and \$c1g - \$c7g and reset them all back to 0. but i still had the problem

Share on other sites

Try this,

Every time you perform a calculation use ConsoleWrite() before and after the equation to check the values are being processed correctly.

Share on other sites

@Jos

ok i added the line MsgBox(0, "TEST", \$cgt & " / " & \$cct) right before the \$GPA = \$cgt / \$cct

the first time around it will work correctly, i put in the nnumbers fromt he example i gave and it ouputs, 34 / 10. but if i try to calculate again, no matter what i put int he boxes, it outputs 0 / 0

So I was right about the fact you are dividing by 0 The reason is simple looking at your script again.

This piece of code screws up your Control handles:

```\$c1c = GUICtrlRead(\$c1c)

The Variables at the left side of the = should be different form the variable used when the control was created because else the Handle is overridden by the value of the control.

Jos

Edited by Jos

Live for the present,
Dream of the future,
Learn from the past. Share on other sites

Here is some formatting to your script I have also put the functions at the bottom. Oh and the Call function is gone ```#include <GUIConstants.au3>

\$NumClass = 0
\$button_calc_y = 0
\$GUI_height = 0

GUICreate("GPA", 120, \$GUI_height)
GUISetState (@SW_SHOW)
;-----------------------------------------------------------

GuiCtrlCreateLabel("Credits",10,10)

\$Button_calc = GUICtrlCreateButton ("Calculate",30, \$button_calc_y, 60, 20)

If \$NumClass > 1 Then
\$c1c = GUICtrlCreateInput ("",10, 30,45,20)
\$c1g = GUICtrlCreateInput ("",65, 30,45,20)

\$c2c = GUICtrlCreateInput ("",10, 55,45,20)
\$c2g = GUICtrlCreateInput ("",65, 55,45,20)
EndIf

If \$NumClass > 2 Then
\$c3c = GUICtrlCreateInput ("",10, 80,45,20)
\$c3g = GUICtrlCreateInput ("",65, 80,45,20)
EndIf

If \$NumClass > 3 Then
\$c4c = GUICtrlCreateInput ("",10, 105,45,20)
\$c4g = GUICtrlCreateInput ("",65, 105,45,20)
EndIf

If \$NumClass > 4 Then
\$c5c = GUICtrlCreateInput ("",10, 130,45,20)
\$c5g = GUICtrlCreateInput ("",65, 130,45,20)
EndIf

If \$NumClass > 5 Then
\$c6c = GUICtrlCreateInput ("",10, 155,45,20)
\$c6g = GUICtrlCreateInput ("",65, 155,45,20)
EndIf

If \$NumClass > 6 Then
\$c7c = GUICtrlCreateInput ("",10, 180,45,20)
\$c7g = GUICtrlCreateInput ("",65, 180,45,20)
EndIf

Func Calculate()
If \$NumClass > 1 Then
\$cct = \$c1c + \$c2c
\$cgt = (\$c1c * \$c1g) + (\$c2c * \$c2g)
EndIf
If \$NumClass > 2 Then
\$cct = \$cct + \$c3c
\$cgt = \$cgt + (\$c3c * \$c3g)
EndIf
If \$NumClass > 3 Then
\$cct = \$cct + \$c4c
\$cgt = \$cgt + (\$c4c * \$c4g)
EndIf
If \$NumClass > 4 Then
\$cct = \$cct + \$c5c
\$cgt = \$cgt + (\$c5c * \$c5g)
EndIf
If \$NumClass > 5 Then
\$cct = \$cct + \$c6c
\$cgt = \$cgt + (\$c6c * \$c6g)
EndIf
If \$NumClass > 6 Then
\$cct = \$cct + \$c7c
\$cgt = \$cgt + (\$c7c * \$c7g)
EndIf
\$GPA = \$cgt / \$cct

MsgBox(0, "GPA Calculator", "Your GPA is: " & \$GPA)
EndFunc

;-----------------------------------------------------------
While 1
\$msg = GUIGetMsg()
Select
Case \$msg = \$GUI_EVENT_CLOSE
ExitLoop
Case \$msg = \$Button_calc
Calculate()
EndSelect
Wend

\$NumClass = InputBox("GPA Calculator", "How many classes are you taking? (2-7)", "", "", 220, 120)
Check()
\$button_calc_y = ((\$NumClass - 2) * 25) + 85
\$GUI_height = \$button_calc_y + 30
EndFunc

Func Check()
If \$NumClass > 7 Then
MsgBox(0,"GPA - Error", "Invalid Amount of Classes, must be between 2 and 7")
EndIf
If \$NumClass < 2 Then
MsgBox(0,"GPA - Error", "Invalid Amount of Classes, must be between 2 and 7")
EndIf
EndFunc```

Share on other sites

well i kinda figured that the variable would overwrite itself with the new input value so their would be no problem, but i will change the variable names and ill post back, thanks for the help

Edited by ragnarok775

Share on other sites

well i kinda figured that the variable would overwrite itself with the new input value so their would be no problem, but i will change the variable names and ill post back, thanks for the help

That a wrong way of looking at it.

At the GUICtrlCreate....() time the function will return a HANDLE that always needs to be used for the just created Control and has NOITHING to do with it Controls content (Value). To retrieve the value for the control you do GUICtrlRead(\$Handle_of_theControl) .

Does that make it clearer or did I only confuse you now ? Live for the present,
Dream of the future,
Learn from the past. Share on other sites

It works fine now, Thank you very much... as i said before, i have no programming experience, just using the help file and the forums <3 . im gonna clean up the GUI maybe make it look alittle nice and then upload it again. just incase someone wants a GPA Calculator =P