Sign in to follow this  
Followers 0
El-Trucha

I need some help with arrays...:P

10 posts in this topic

Hello everyone!! ;)

OK...here's my code:

For $x = 1 To $classes ; $classes is a number...:idiot:

Dim $class[$x] = InputBox("What grade?", "What's your grade for class #" & $x & "??")

$x + 1

Next

It tells me the following error when I get to that loop:

Line 18 (File "<not_gonna_tell_u>"):

Dim $class[$x] = InputBox("What grade?", "What's your grade for class #" & $x & "??")

Dim $class[$x] ^ ERROR

Error: Array variable subscript badly formatted.

Why is it giving me that?? :D

Thanx!! :lol:


El-Truchahttp://www.truchasoft.tk[url="ftp://tsfc.homeftp.net"]ftp://tsfc.homeftp.net[/url]hotline://tsfc.ath.cx

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

You have to declare an array before you use it. By the way, arrays normally start with index 0 so Dim $array[10] would create elements 0 thru 9. If you want an element with index 10 you need to Dim $a[11].

Also, I'm not sure why you have $x + 1 in your For loop, because $x is incremented automatically and you don't assign the value back to anything.

Dim $class[$classes+1] 
For $x = 1 To $classes; $classes is a number...tongue.gif
   $class[$x] = InputBox("What grade?", "What's your grade for class #" & $x & "??")
Next
Edited by Larry

Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!

Share this post


Link to post
Share on other sites

Larry, CyberSlug, theres probably no harm in the "Dim $class[$x]" you both left in the For loop, but I think its a very bad example to leave. There also may be a performance hit as a new array is Dim'ed every time before the array is overwritten by the InputBox() return value. Also, looking closer, Larry's code my not even run as Dim $array[0] is illegal, but that is what will happen during the first iteration of the loop.

Share this post


Link to post
Share on other sites

Yay it worked!! :D

Thanx u all!! :lol:

Now...I have another question: how do u make it return a decimal number with periods?? because:

1 + 3 + 2 + 1 = 7 / 4 = 1.75

And it just returns 1...:D

How do I fix that?? :idiot:

Thanx!! ;)


El-Truchahttp://www.truchasoft.tk[url="ftp://tsfc.homeftp.net"]ftp://tsfc.homeftp.net[/url]hotline://tsfc.ath.cx

Share this post


Link to post
Share on other sites

I just made one of those, it's in the scripts and scraps forum thing. =-o


My site for HTML Help :)[quote name='Valik' date='Oct 15 2004, 12:29 PM']Maybe nobody is an "elite uber-coder" like me because thinking is a capital offense in today's online-world?[right][snapback]36427[/snapback][/right][/quote]

Share this post


Link to post
Share on other sites

Now...I have another question: how do u make it return a decimal number with periods?? because:

1 + 3 + 2 + 1 = 7 / 4 = 1.75

And it just returns 1...:D

How do I fix that?? :idiot:

Thanx!! :lol:

<{POST_SNAPBACK}>

when does it return only a 1 ? can you show the code that does ?

Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

Yes, here:

$x = 1

Dim $grade

For $x = 1 To $classes

$grade = $grade + $class[$x]

Next

$finalGrade = $grade / $classes

Or if u'd like to see the whole thing, it's in the attachment...:idiot:

GPACalc.au3


El-Truchahttp://www.truchasoft.tk[url="ftp://tsfc.homeftp.net"]ftp://tsfc.homeftp.net[/url]hotline://tsfc.ath.cx

Share this post


Link to post
Share on other sites

Nope, still get 1...thanx anyways...:idiot:


El-Truchahttp://www.truchasoft.tk[url="ftp://tsfc.homeftp.net"]ftp://tsfc.homeftp.net[/url]hotline://tsfc.ath.cx

Share this post


Link to post
Share on other sites

Yay it works!! ;):D

It says 1.75...thanx!! :D

But...I do this:

$finalGrade = $grade / $classes

MsgBox(4096, "", $finalGrade)

And it says the correct number, but then I do this:

$message = StringFormat("Your GPA is %d, that means you got a(n) %s!!", $finalGrade, $gradeLetter)

MsgBox(4096, "Your GPA...", $message)

Which is after the first code, and it says only 1!! :idiot:

Why??

Thanx!! :lol:


El-Truchahttp://www.truchasoft.tk[url="ftp://tsfc.homeftp.net"]ftp://tsfc.homeftp.net[/url]hotline://tsfc.ath.cx

Share this post


Link to post
Share on other sites

That's what I was doing!! :idiot:

Yah, I tried %g and it worked!! :lol:

Much, much thanx!! :D


El-Truchahttp://www.truchasoft.tk[url="ftp://tsfc.homeftp.net"]ftp://tsfc.homeftp.net[/url]hotline://tsfc.ath.cx

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0