Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 VB 6 - Differentiating from characters to numbers (simplistic)
Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
isaiahk9




PostPosted: Wed Oct 08, 2008 3:06 pm   Post subject: VB 6 - Differentiating from characters to numbers (simplistic)

Like this title says, this is a simplistic question. Hopefully with a simplistic answer. I have just started on VB 6 and have run into a problem :
I need to make a program that takes in the user's number of community service hours through a text box, and then output answers :
"That's enough, you have over 40 hours."
For, when you enter at least 40 hours.
"You need " & 40 - number of hours & " hours."
For when you are greater than 0.
"You cannot enter a negative number."
When the user is an idiot and enters a negative number.
"That's not a number of hours."
For when the user enters a non-integer like a string or decimal.

So here is the code :
' This Private Sub activates whenever the GO! button is clicked
Private Sub cmdGo_Click()
strHours = txtEnter.Text
' If the user has 40 hours, they are congratulated
If strHours >= 40 Then
lblDisplay.Caption = "You're done - you need no more hours!"
' If the user has less than 40 hours, then they're told how much they need
ElseIf strHours >= 0 Then
' This variable changes depending on the number of hours you currently have
intRemainingHours = 40 - strHours
lblDisplay.Caption = "You need " & intRemainingHours & " hours more to finish."
' If the user enters a negative number, the program will catch them
ElseIf strHours < 0 Then
lblDisplay.Caption = "You can't have a negative amount of time."
' Otherwise, the user has not entered a number, and they're told so
Else
lblDisplay.Caption = "That's not a number of hours."
End If
End Sub


But the program just stops to debug whenever you enter a non-integer (because it cannot be greater than or less than 40, so the error line appears). How can I make the program respond to when the user enters a non-integer. Any idea how?
thanx
Sponsor
Sponsor
Sponsor
sponsor
unoho




PostPosted: Wed Oct 08, 2008 3:45 pm   Post subject: RE:VB 6 - Differentiating from characters to numbers (simplistic)

well.. the reason it's happening is that because entering a string, not an integer.

you can't compare a string with a number. so.. change the strHours to intHours and run it. it should work.
isaiahk9




PostPosted: Wed Oct 08, 2008 5:03 pm   Post subject: RE:VB 6 - Differentiating from characters to numbers (simplistic)

no, I'm pretty sure that's not it. Originally the program was using intHours anyways - and the bug still existed. You can compare a string to a number if the string is a number. The program works for any mark that is a number, but when the input is not a number, the program will not work.
isaiahk9




PostPosted: Wed Oct 08, 2008 5:03 pm   Post subject: RE:VB 6 - Differentiating from characters to numbers (simplistic)

no, I'm pretty sure that's not it. Originally the program was using intHours anyways - and the bug still existed. You can compare a string to a number if the string is a number. The program works for any mark that is a number, but when the input is not a number, the program will not work. thanx anyway
jbking




PostPosted: Wed Oct 08, 2008 6:52 pm   Post subject: RE:VB 6 - Differentiating from characters to numbers (simplistic)

Would the function IsNumeric() or CInt() serve the purpose you want or do you want their implementation details?

If the latter, may I suggest using a regular expression to determine validity.

JB
isaiahk9




PostPosted: Wed Oct 08, 2008 7:27 pm   Post subject: RE:VB 6 - Differentiating from characters to numbers (simplistic)

Like I said, I just started VB, so what is IsNumeric() and CInt()?
jbking




PostPosted: Thu Oct 09, 2008 10:34 am   Post subject: Re: VB 6 - Differentiating from characters to numbers (simplistic)

IsNumeric() is the boolean function that takes a string and tries to see if the value is a number. I'm not sure it is in VB6 but I have seen it in VBScript and VB.Net and think it may have been grandfathered in(Meaning it existed in the older versions and thus has to exist in newer ones too).

Cint() meanwhile is a converting, likely very similar if not identical to a casting, function that tries to take the input and return an integer, possibly rounded, that was requested. So, if you used 1.5 as a parameter, CInt would return 2. However, if you entered "10" it would return 10. This is slightly different in that it will return the value if the parameter rather than act as a check to see that it can be done.

So, you could have the following code:

code:
If (IsNumeric(VarToTest)) then
    MyInt = CInt(VarToTest)
End If



You may want to try searching on MSDN for some help, which may be challenging given all the Visual Basic versions Microsoft has released, in finding various VB functions or try VBScript reference that may have some of the same functions as VB6.
isaiahk9




PostPosted: Thu Oct 09, 2008 4:01 pm   Post subject: RE:VB 6 - Differentiating from characters to numbers (simplistic)

thanx, that's exactly what I was looking for. problem solved.
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Visual Basic and Other Basics -> Visual Basic Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 8 Posts ]
Jump to:   


Style:  
Search: