Results 1 to 5 of 5

Thread: Help! Infinite loop!

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Nov 1999
    Location
    Baytown, TX United States
    Posts
    19

    Post

    The following code gives me an infinite loop. How should I change this so it won't be infinite?

    Dim curSales as Currency
    curSales = Val(Inputbox("Enter a sales amount")
    Do While curSales > 0
    Print curSales * .1
    Loop



  2. #2
    Hyperactive Member compuGEEK's Avatar
    Join Date
    May 1999
    Location
    Mpls,MN,USA
    Posts
    281

    Post






    [This message has been edited by compuGEEK (edited 11-22-1999).]

  3. #3
    Hyperactive Member compuGEEK's Avatar
    Join Date
    May 1999
    Location
    Mpls,MN,USA
    Posts
    281

    Post

    Oops.......

    Dim curSales As Currency
    Dim y As Integer
    curSales = Val(InputBox("enter a number:"))

    For y = 1 To curSales
    Print curSales * 0.1
    Next y



  4. #4
    Hyperactive Member
    Join Date
    May 1999
    Location
    Sunny Southern Weather
    Posts
    406

    Post

    I don't think I'd use 'dim y as integer' since it appears that 'currency' could be a decimal number.
    As well, that solution will 'for each value from 1 to cursales' - so if I enter 100 as cursales, I'll get 100 print lines.....
    It depends on what your trying to achieve.
    If you want to query the user until they enter '0' try this:
    Dim curSales as Currency
    cursales=1
    Do While curSales > 0
    curSales = Val(Inputbox("Enter a sales amount")
    if cursales > 0 then
    Print curSales * .1
    else
    exit do
    end if
    Loop
    (it's a bit wordy, but it's early in the morning...)
    However, if you only want to ask them once, perhaps by way of a command button or some other call to the procedure, try this:
    Dim curSales as Currency
    curSales = Val(Inputbox("Enter a sales amount")
    if curSales > 0 then
    Print curSales * .1
    end if

    One more thing:
    You really should do your own homework. If you can't figure something out, don't post requests - go to class and learn the solutions from the teacher or the clues the teacher gives. Then come back here and see if there are additional solutions.
    I've been a teacher for over 12 years and there's nothing worse than a student who doesn't do his own work and can't say "I don't know"!

  5. #5
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892

    Post

    Print does not change any variable. curSales isn't affected by it, so if you entered the loop, you can't escape.

    Replace this:
    Print curSales * .1

    With this:
    curSales = curSales * .1
    Print curSales


    Mathematically it is still an infinite loop, but VB can't handle decimal fractions this long, so sooner or later, curSales will become zero and the loop will end.

    ------------------
    Yonatan
    Teenage Programmer
    E-Mail: [email protected]
    ICQ: 19552879
    AIM: RYoni69

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width