I need some VB6 help!!!!!!!!!!!!!!!

Ok I need to hack out some VB6 code for this project at school, I am having a problem setting up the array.
What I am doing is very simple, the form has one button that says,"click me".
Once you click the button a input box pops up and asks, "are you 0 years old?", and your answer could be either yes/No. If you answer yes, a MSGbox appears and says, "you are 0 years old!". If you answer No, the input box continues with, "are you 1 years old?" and so on until your answer is yes.

I got the code worked out but i cannot seem to get the code to display a new number each time you answer no.

The way it is working now, is that when you answer no, it loops but with the same number (IE : "are you 0 years old?" answer:no, then the imput box appears again and says,"are you 0 years old?", and keeps repeating the same thing.)
Thats were i am having my problems. I know its gotta be a simple loop or a if-then-else, but i cant figure it out!!

any help would be great! :)


Comments

  • I don't want to do your homework for you, so I'll give you some hints. :-)

    - You need to use a local loop counter variable that you increment each time the loop goes around.

    - You need to concatenate the value of the loop counter variable in the message you are sending to the MsgBox() function.

    Take care,
    Daniel Read
    [b]developer.*[/b]
    http://www.developerdotstar.com


  • Thank you very much for the help!
    I cant believe I spent 4 hours (with the help of 3 other people)to figure it out.

    But with your help it dawned on me...and the answer was so simple it hurt!

    Thanks !!!!!! a million!

    Here is the actual code:
    all it has on the form is a button!

    Private Sub Command1_Click()
    Dim response As Integer, findage As String


    Do Until findage = "y"
    response = response + 1
    findage = InputBox("are you" + " " + Str(response) + " " + "years OLD? fool.")
    Loop
    If findage = "y" Then
    MsgBox ("you are" + " " + Str(response) + " " + "old, fool")

    End If

    End Sub

    :


  • James,

    Great job coming up with the solution. Now that you've got your code, let me help you improve it a bit. I know this is just homework, but you can impress your instructor with a little polish.

    First, it's a good idea to give your button a name other than Command1. Normally you would use the prefix of "cmd" on the name. Something like "cmdAskForAge" might work, or you can come up with something better based on the other controls on your form.

    Second, it's a good idea to use "type prefixes" in front of your variable names. For example, use iResponse and sFindAge. You can look in the VB documentation for a complete list of these prefixes. Search for the words "Hungarian" and "prefix".

    Third, I would change your Do loop to make it explicit that you intend for your loop to always execute at least once. For this, I like to use this syntax:

    [code]
    Do

    ...

    Loop While bAskAgain = True
    [/code]

    Fourth, notice above that I have added a variable called bAskAgain. The b prefix indicates that it's a Boolean variable. This makes the code more explicit, and also relates to the next item:

    Fifth, one thing you always want to try to do is code defensively, and limit your implicit assumptions as much as possible. Your code is making an implicit assumption that the user is going to know exactly to type in a lower case letter "y" as an afirmative repsonse. What is she types in an upper case "Y", or types in the word "Yes" or "yes" or "YES"? Wouldn't it be better if your code checked for at least a set of the most likely afirmative responses? Also, your code is also assuming that any afirmative response you don't recognize equates to a negative response. This reduces the "user friendliness" of your code.

    I don't want to write the code for you, but try using the UCase$() function to eliminate case sensitivity issues from your comparison, and also try using "Or" in your If statement to check for multiple responses. Here's another option that would totally solve this question of negative/positive responses: try using the MsgBox() function, which can give you a Yes and No button, and return a different value based on which button the user clicked. That removes all ambiguity. However, using MsgBox() for your prompt might be outside of the scope of your assignment, so maybe you can't do that. If you're forced to use InputBox(), try at least adding some additional text to your prompt to tell the user what valid affirmative and negative responses are.

    Sixth, instead of using the "+" operator to concatenate your strings, it is always preferrable to use the "&" operator. Limit the use of the "+" operator to math operations on numbers and numeric variables. Also, you don't have to have the extra concatenations for the spaces (" "); just put the spaces at the end of the strings, like this:

    "You are " & iResponse & " years old."

    Also notice that using the & operator removes the need to use CStr().

    Well this turned into a longer post than I had intended. :-) But I think you're off to a good start as a programmer. Remember that there are always many "correct" answers to a programming problem, and you want to strive to find the "best" correct answer. The most important thing is to try to figure out things that could go wrong, and try to use techniques that will prevent those things from going wrong. The second most important thing is to write code that is clear and readable, with good indenting techniques, good variable naming techniques, and always favoring the explicit over the implicit.


    Take care,
    Daniel Read
    [b]developer.*[/b]
    http://www.developerdotstar.com


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories