none
arrays and asking to run program again RRS feed

  • Question

  • i have an assignment where i need to make the program ask the user if they want to start over again.

    When the analysis is complete, ask the user if he or she wants to run it again.

    i can not figure out how to do that part. I am working with arrays. A tutor told me to do a while loop but it isn't working. What do i do? Any ideas?

    should say this and either run the program again or end it:   Would you like to run the program again (enter “yes” or “no”) ? no

    Sunday, October 18, 2015 4:40 PM

Answers

  • The while loop should be effectively 'while the user wants to continue'.

    So inside the loop you need to set a variable from the user that indicates you want to continue (repeat the loop).

    We don't do homework for you, so if you want to post your code so far we can help with suggestions to help you get there.

    Do you really hate programming?

    Sunday, October 18, 2015 4:48 PM
    Moderator
  • Ok, here is a small sample to help with the while loop and ending it when the user is done.

    doContinue= "True" 'Initially we want to start the program
    
    While (doContinue) 'Keep going doing stuff until doContinue is not "true"
      TextWindow.WriteLine("BlaBlaBla") ' Do something
      
      TextWindow.WriteLine("Do you want to do it again? (y/n)")
      input = TextWindow.Read() ' get input
      input = Text.GetSubText(input,1,1) ' just get first character
      input = Text.ConvertToLowerCase(input) ' convert to lower case y
      If (input <> "y") Then ' anythng apart from something starting with y or Y will end the While loop
        doContinue = "False"
      EndIf
    EndWhile
    Why do you hate programing - you seem quite good at it?
    Sunday, October 18, 2015 5:21 PM
    Moderator
  • That is where you would put the code that you want to be done very time to user wants to 'do it again'. 

    This is the savings code - be careful to only put the bits you want to reat here - perhaps not the user name or anything else that doesn't change each time the user does it again in the while loop.

    Sunday, October 18, 2015 5:38 PM
    Moderator
  • You need to put ALL of the stuff you want to repeat INSIDE the while loop.

    The code will repeat what is inside the WHILE loop while the while condition (doContinue) is true.

    'create array for month of year 
    monthsInYear = "1=January;2=February;3=March;4=April;5=May;6=June;7=July;8=August;9=September;10=October;11=November;12=December;"
    
    
    'ask for user name
    TextWindow.Write("enter name of business: ")
    businessName = TextWindow.Read()
    
    doContinue= "True" 'Initially we want to start the program
    While (doContinue) 'Keep going doing stuff until doContinue is not "true"
      
      'ask for projected savings
      TextWindow.Write("enter what you hope to save: ")
      projectedSavings = TextWindow.ReadNumber()
      
      'get bills without garden
      TextWindow.WriteLine("Enter the bills without garden.")
      For i = 1 To 12
        TextWindow.Write(monthsInYear[i] + ": ")
        withoutGarden[i] = TextWindow.ReadNumber()
        totalWithoutGarden = totalWithoutGarden + withoutGarden[i]
      EndFor
      
      'get bills with garden
      TextWindow.WriteLine("Enter the bills with garden.")
      For i = 1 to 12
        TextWindow.Write(monthsInYear[i] + ": ")
        withGarden[i] = TextWindow.ReadNumber()
        totalWithGarden = totalWithGarden + withGarden[i]
      EndFor
      
      'calculate savings
      For i = 1 to 12
        savings[i] = withoutGarden[i] - withGarden[i]
        totalSavings = totalSavings + savings[i]
      EndFor
      
      'display results
      TextWindow.WriteLine("Month Without With Savings")
      TextWindow.WriteLine("-----------------------------")
      For i = 1 To 12
        TextWindow.WriteLine(monthsInYear[i]  +  "   "  +  withoutGarden[i]  +  "   "  +  withGarden[i]  +   "   "   +  savings[i])
        'Display totals
      EndFor
      TextWindow.WriteLine("-----------------------------")
      
      TextWindow.Writeline("Totals: " + totalWithoutGarden + "  " + totalWithGarden + "  " + totalSavings)
      
      TextWindow.WriteLine("-----------------------------")
      
      'outputs the total savings, projected saving and the difference
      TextWindow.WriteLine("Savings Analysis ")
      TextWindow.WriteLine("-----------------------------")
      TextWindow.WriteLine("Actual Savings: " + totalSavings)
      TextWindow.WriteLine("Projected Savings: " + projectedSavings)
      TextWindow.WriteLine("Difference: " + (totalSavings - projectedSavings))
      'tells you if you saved money or lost it depending on what you had projected to spend.
      If projectedSavings <= totalSavings Then
        TextWindow.WriteLine(" you saved money!")
      EndIf  
      If  projectedSavings > totalSavings then 
        TextWindow.WriteLine(" you lost money, sorry")
        
      EndIf
      
      
      
      TextWindow.WriteLine("Do you want to do it again? (y/n)")
      input = TextWindow.Read() ' get input
      input = Text.GetSubText(input,1,1) ' just get first character
      input = Text.ConvertToLowerCase(input) ' convert to lower case y
      If (input <> "y") Then ' anythng apart from something starting with y or Y will end the While loop
        doContinue = "False"
      EndIf
    EndWhile


    Sunday, October 18, 2015 5:52 PM
    Moderator
  • its just the same idea, while invalid data repeat

    dataInvalid = "True"
    While (dataInvalid)
      TextWindow.WriteLine("Enter a nmber > 0")
      input = TextWindow.ReadNumber()
      If (input > 0) Then
        dataInvalid = "False"
      Else
        TextWindow.WriteLine("please do it right")
      EndIf
    EndWhile
    You should be able to use this idea for all input, checking for whatever the valid data is.

    Sunday, October 18, 2015 6:20 PM
    Moderator
  • The if method you use is Ok but it only checks one error - a while will catch multiple errors until the user gets it right

    For i = 1 to 12 TextWindow.Write(monthsInYear[i] + ": ") withGarden[i] = TextWindow.ReadNumber() totalWithGarden = totalWithGarden + withGarden[i] EndFor If WithGarden[i] < 0 Then TextWindow.Writeline(" Error! The value must be greater than 0") TextWindow.Writeline(" Enter correct value: ") WithGarden[i] = TextWindow.ReadNumber() EndIf

    The If should be inside the For loop since you wanto check each entry  If you prefer to use GoTo (While is considered better programing though)

    For i = 1 to 12
      TextWindow.Write(monthsInYear[i] + ": ")
      repeatWithGarden:
      withGarden[i] = TextWindow.ReadNumber()
      If WithGarden[i] < 0 Then 
        TextWindow.Writeline(" Error! The value must be greater than 0")
        TextWindow.Writeline(" Enter correct value: ")
        GoTo repeatWithGarden
      EndIf 
      totalWithGarden = totalWithGarden + withGarden[i]
    EndFor

    Sunday, October 18, 2015 6:34 PM
    Moderator
  • no this forum is where me and others help out - the  reason we do it here is that the questions, answers and discussion can help more than just the asker and provide a back history of questions and replies that can be searched.

    good luck.

    Sunday, October 18, 2015 6:47 PM
    Moderator

All replies

  • The while loop should be effectively 'while the user wants to continue'.

    So inside the loop you need to set a variable from the user that indicates you want to continue (repeat the loop).

    We don't do homework for you, so if you want to post your code so far we can help with suggestions to help you get there.

    Do you really hate programming?

    Sunday, October 18, 2015 4:48 PM
    Moderator
  • The while loop should be effectively 'while the user wants to continue'.

    So inside the loop you need to set a variable from the user that indicates you want to continue (repeat the loop).

    We don't do homework for you, so if you want to post your code so far we can help with suggestions to help you get there.

    Do you really hate programming?


    Question
    You cannot vote on your own post

    i do really hate it and i will post my code for you. I am so confused and just need clearing up.

    'CIS 116
    'Elizabeth Spencer
    '10-14-15 
    'Build a program that uses arrays to store information that is processed to present information to the user
    
    
    
    'create array for month of year 
    monthsInYear = "1=January;2=February;3=March;4=April;5=May;6=June;7=July;8=August;9=September;10=October;11=November;12=December;"
    
    
    'ask for user name
    TextWindow.Write("enter name of business: ")
    businessName = TextWindow.Read()
    
    'ask for projected savings
    TextWindow.Write("enter what you hope to save: ")
    projectedSavings = TextWindow.ReadNumber()
    
    'get bills without garden
    TextWindow.WriteLine("Enter the bills without garden.")
    For i = 1 To 12
      TextWindow.Write(monthsInYear[i] + ": ")
      withoutGarden[i] = TextWindow.ReadNumber()
      totalWithoutGarden = totalWithoutGarden + withoutGarden[i]
    EndFor
    
    'get bills with garden
    TextWindow.WriteLine("Enter the bills with garden.")
    For i = 1 to 12
       TextWindow.Write(monthsInYear[i] + ": ")
       withGarden[i] = TextWindow.ReadNumber()
       totalWithGarden = totalWithGarden + withGarden[i]
    EndFor
    
    'calculate savings
    For i = 1 to 12
      savings[i] = withoutGarden[i] - withGarden[i]
      totalSavings = totalSavings + savings[i]
    EndFor
    
    'display results
    TextWindow.WriteLine("Month Without With Savings")
    TextWindow.WriteLine("-----------------------------")
    For i = 1 To 12
      TextWindow.WriteLine(monthsInYear[i]  +  "   "  +  withoutGarden[i]  +  "   "  +  withGarden[i]  +   "   "   +  savings[i])
    'Display totals
    EndFor
    TextWindow.WriteLine("-----------------------------")
    
    TextWindow.Writeline("Totals: " + totalWithoutGarden + "  " + totalWithGarden + "  " + totalSavings)
    
    TextWindow.WriteLine("-----------------------------")
    
    'outputs the total savings, projected saving and the difference
    TextWindow.WriteLine("Savings Analysis ")
    TextWindow.WriteLine("-----------------------------")
    TextWindow.WriteLine("Actual Savings: " + totalSavings)
    TextWindow.WriteLine("Projected Savings: " + projectedSavings)
    TextWindow.WriteLine("Difference: " + (totalSavings - projectedSavings))
    'tells you if you saved money or lost it depending on what you had projected to spend.
    If projectedSavings <= totalSavings Then
      TextWindow.WriteLine(" you saved money!")
    EndIf  
    If  projectedSavings > totalSavings then 
      TextWindow.WriteLine(" you lost money, sorry")
    
    EndIf
    
    
    
    Sunday, October 18, 2015 5:17 PM
  • Ok, here is a small sample to help with the while loop and ending it when the user is done.

    doContinue= "True" 'Initially we want to start the program
    
    While (doContinue) 'Keep going doing stuff until doContinue is not "true"
      TextWindow.WriteLine("BlaBlaBla") ' Do something
      
      TextWindow.WriteLine("Do you want to do it again? (y/n)")
      input = TextWindow.Read() ' get input
      input = Text.GetSubText(input,1,1) ' just get first character
      input = Text.ConvertToLowerCase(input) ' convert to lower case y
      If (input <> "y") Then ' anythng apart from something starting with y or Y will end the While loop
        doContinue = "False"
      EndIf
    EndWhile
    Why do you hate programing - you seem quite good at it?
    Sunday, October 18, 2015 5:21 PM
    Moderator
  • Ok, here is a small sample to help with the while loop and ending it when the user is done.

    doContinue= "True" 'Initially we want to start the program
    
    While (doContinue) 'Keep going doing stuff until doContinue is not "true"
      TextWindow.WriteLine("BlaBlaBla") ' Do something
      
      TextWindow.WriteLine("Do you want to do it again? (y/n)")
      input = TextWindow.Read() ' get input
      input = Text.GetSubText(input,1,1) ' just get first character
      input = Text.ConvertToLowerCase(input) ' convert to lower case y
      If (input <> "y") Then ' anythng apart from something starting with y or Y will end the While loop
        doContinue = "False"
      EndIf
    EndWhile
    Why do you hate programing - you seem quite good at it?

    can i use that in my program but just change words? and to me programming is super hard

    Sunday, October 18, 2015 5:26 PM
  • if you understand how it works and you change the words you can use it
    Sunday, October 18, 2015 5:30 PM
    Moderator
  • if you understand how it works and you change the words you can use it

    I do understand it but what would i do in the part where you said blahblahblah? im confused as to what it could really say? everything else is clear and should work just fine how you have it.

    Sunday, October 18, 2015 5:33 PM
  • if you understand how it works and you change the words you can use it

    is there another way you can help me where we can chat easier and faster because i need a lot of help finishing my program
    Sunday, October 18, 2015 5:36 PM
  • That is where you would put the code that you want to be done very time to user wants to 'do it again'. 

    This is the savings code - be careful to only put the bits you want to reat here - perhaps not the user name or anything else that doesn't change each time the user does it again in the while loop.

    Sunday, October 18, 2015 5:38 PM
    Moderator
  • That is where you would put the code that you want to be done very time to user wants to 'do it again'. 

    This is the savings code - be careful to only put the bits you want to reat here - perhaps not the user name or anything else that doesn't change each time the user does it again in the while loop.


    i ran the program but it doesnt go back and do the entire program over again if i put a y. It just keeps asking if i want to run the program again. what could i do to make the program start over at the begining if i say yes? saying no works correctly.
    Sunday, October 18, 2015 5:42 PM
  • when you press y the while loop will repeat - so we woul need to see the code to help why it isn't working as yu want.
    Sunday, October 18, 2015 5:44 PM
    Moderator
  • when you press y the while loop will repeat - so we woul need to see the code to help why it isn't working as yu want.

    i want the while loop to bring me back to the start of my program so i can run it again.
    'CIS 116
    'Elizabeth Spencer
    '10-14-15 
    'Build a program that uses arrays to store information that is processed to present information to the user
    
    
    'create array for month of year 
    monthsInYear = "1=January;2=February;3=March;4=April;5=May;6=June;7=July;8=August;9=September;10=October;11=November;12=December;"
    
    
    'ask for user name
    TextWindow.Write("enter name of business: ")
    businessName = TextWindow.Read()
    
    'ask for projected savings
    TextWindow.Write("enter what you hope to save: ")
    projectedSavings = TextWindow.ReadNumber()
    
    'get bills without garden
    TextWindow.WriteLine("Enter the bills without garden.")
    For i = 1 To 12
      TextWindow.Write(monthsInYear[i] + ": ")
      withoutGarden[i] = TextWindow.ReadNumber()
      totalWithoutGarden = totalWithoutGarden + withoutGarden[i]
    EndFor
    
    'get bills with garden
    TextWindow.WriteLine("Enter the bills with garden.")
    For i = 1 to 12
       TextWindow.Write(monthsInYear[i] + ": ")
       withGarden[i] = TextWindow.ReadNumber()
       totalWithGarden = totalWithGarden + withGarden[i]
    EndFor
    
    'calculate savings
    For i = 1 to 12
      savings[i] = withoutGarden[i] - withGarden[i]
      totalSavings = totalSavings + savings[i]
    EndFor
    
    'display results
    TextWindow.WriteLine("Month Without With Savings")
    TextWindow.WriteLine("-----------------------------")
    For i = 1 To 12
      TextWindow.WriteLine(monthsInYear[i]  +  "   "  +  withoutGarden[i]  +  "   "  +  withGarden[i]  +   "   "   +  savings[i])
    'Display totals
    EndFor
    TextWindow.WriteLine("-----------------------------")
    
    TextWindow.Writeline("Totals: " + totalWithoutGarden + "  " + totalWithGarden + "  " + totalSavings)
    
    TextWindow.WriteLine("-----------------------------")
    
    'outputs the total savings, projected saving and the difference
    TextWindow.WriteLine("Savings Analysis ")
    TextWindow.WriteLine("-----------------------------")
    TextWindow.WriteLine("Actual Savings: " + totalSavings)
    TextWindow.WriteLine("Projected Savings: " + projectedSavings)
    TextWindow.WriteLine("Difference: " + (totalSavings - projectedSavings))
    'tells you if you saved money or lost it depending on what you had projected to spend.
    If projectedSavings <= totalSavings Then
      TextWindow.WriteLine(" you saved money!")
    EndIf  
    If  projectedSavings > totalSavings then 
      TextWindow.WriteLine(" you lost money, sorry")
    
    EndIf
    
    
    doContinue= "True" 'Initially we want to start the program
    
    While (doContinue) 'Keep going doing stuff until doContinue is not "true"
      
      TextWindow.WriteLine("Do you want to do it again? (y/n)")
      input = TextWindow.Read() ' get input
      input = Text.GetSubText(input,1,1) ' just get first character
      input = Text.ConvertToLowerCase(input) ' convert to lower case y
      If (input <> "y") Then ' anythng apart from something starting with y or Y will end the While loop
        doContinue = "False"
      EndIf
    EndWhile

    it needs to go back to the begining and not keep asking if they want to start again.
    Sunday, October 18, 2015 5:47 PM
  • You need to put ALL of the stuff you want to repeat INSIDE the while loop.

    The code will repeat what is inside the WHILE loop while the while condition (doContinue) is true.

    'create array for month of year 
    monthsInYear = "1=January;2=February;3=March;4=April;5=May;6=June;7=July;8=August;9=September;10=October;11=November;12=December;"
    
    
    'ask for user name
    TextWindow.Write("enter name of business: ")
    businessName = TextWindow.Read()
    
    doContinue= "True" 'Initially we want to start the program
    While (doContinue) 'Keep going doing stuff until doContinue is not "true"
      
      'ask for projected savings
      TextWindow.Write("enter what you hope to save: ")
      projectedSavings = TextWindow.ReadNumber()
      
      'get bills without garden
      TextWindow.WriteLine("Enter the bills without garden.")
      For i = 1 To 12
        TextWindow.Write(monthsInYear[i] + ": ")
        withoutGarden[i] = TextWindow.ReadNumber()
        totalWithoutGarden = totalWithoutGarden + withoutGarden[i]
      EndFor
      
      'get bills with garden
      TextWindow.WriteLine("Enter the bills with garden.")
      For i = 1 to 12
        TextWindow.Write(monthsInYear[i] + ": ")
        withGarden[i] = TextWindow.ReadNumber()
        totalWithGarden = totalWithGarden + withGarden[i]
      EndFor
      
      'calculate savings
      For i = 1 to 12
        savings[i] = withoutGarden[i] - withGarden[i]
        totalSavings = totalSavings + savings[i]
      EndFor
      
      'display results
      TextWindow.WriteLine("Month Without With Savings")
      TextWindow.WriteLine("-----------------------------")
      For i = 1 To 12
        TextWindow.WriteLine(monthsInYear[i]  +  "   "  +  withoutGarden[i]  +  "   "  +  withGarden[i]  +   "   "   +  savings[i])
        'Display totals
      EndFor
      TextWindow.WriteLine("-----------------------------")
      
      TextWindow.Writeline("Totals: " + totalWithoutGarden + "  " + totalWithGarden + "  " + totalSavings)
      
      TextWindow.WriteLine("-----------------------------")
      
      'outputs the total savings, projected saving and the difference
      TextWindow.WriteLine("Savings Analysis ")
      TextWindow.WriteLine("-----------------------------")
      TextWindow.WriteLine("Actual Savings: " + totalSavings)
      TextWindow.WriteLine("Projected Savings: " + projectedSavings)
      TextWindow.WriteLine("Difference: " + (totalSavings - projectedSavings))
      'tells you if you saved money or lost it depending on what you had projected to spend.
      If projectedSavings <= totalSavings Then
        TextWindow.WriteLine(" you saved money!")
      EndIf  
      If  projectedSavings > totalSavings then 
        TextWindow.WriteLine(" you lost money, sorry")
        
      EndIf
      
      
      
      TextWindow.WriteLine("Do you want to do it again? (y/n)")
      input = TextWindow.Read() ' get input
      input = Text.GetSubText(input,1,1) ' just get first character
      input = Text.ConvertToLowerCase(input) ' convert to lower case y
      If (input <> "y") Then ' anythng apart from something starting with y or Y will end the While loop
        doContinue = "False"
      EndIf
    EndWhile


    Sunday, October 18, 2015 5:52 PM
    Moderator
  • You need to put ALL of the stuff you want to repeat INSIDE the while loop.

    The code will repeat what is inside the WHILE loop while the while condition (doContinue) is true.

    'create array for month of year 
    monthsInYear = "1=January;2=February;3=March;4=April;5=May;6=June;7=July;8=August;9=September;10=October;11=November;12=December;"
    
    
    'ask for user name
    TextWindow.Write("enter name of business: ")
    businessName = TextWindow.Read()
    
    doContinue= "True" 'Initially we want to start the program
    While (doContinue) 'Keep going doing stuff until doContinue is not "true"
      
      'ask for projected savings
      TextWindow.Write("enter what you hope to save: ")
      projectedSavings = TextWindow.ReadNumber()
      
      'get bills without garden
      TextWindow.WriteLine("Enter the bills without garden.")
      For i = 1 To 12
        TextWindow.Write(monthsInYear[i] + ": ")
        withoutGarden[i] = TextWindow.ReadNumber()
        totalWithoutGarden = totalWithoutGarden + withoutGarden[i]
      EndFor
      
      'get bills with garden
      TextWindow.WriteLine("Enter the bills with garden.")
      For i = 1 to 12
        TextWindow.Write(monthsInYear[i] + ": ")
        withGarden[i] = TextWindow.ReadNumber()
        totalWithGarden = totalWithGarden + withGarden[i]
      EndFor
      
      'calculate savings
      For i = 1 to 12
        savings[i] = withoutGarden[i] - withGarden[i]
        totalSavings = totalSavings + savings[i]
      EndFor
      
      'display results
      TextWindow.WriteLine("Month Without With Savings")
      TextWindow.WriteLine("-----------------------------")
      For i = 1 To 12
        TextWindow.WriteLine(monthsInYear[i]  +  "   "  +  withoutGarden[i]  +  "   "  +  withGarden[i]  +   "   "   +  savings[i])
        'Display totals
      EndFor
      TextWindow.WriteLine("-----------------------------")
      
      TextWindow.Writeline("Totals: " + totalWithoutGarden + "  " + totalWithGarden + "  " + totalSavings)
      
      TextWindow.WriteLine("-----------------------------")
      
      'outputs the total savings, projected saving and the difference
      TextWindow.WriteLine("Savings Analysis ")
      TextWindow.WriteLine("-----------------------------")
      TextWindow.WriteLine("Actual Savings: " + totalSavings)
      TextWindow.WriteLine("Projected Savings: " + projectedSavings)
      TextWindow.WriteLine("Difference: " + (totalSavings - projectedSavings))
      'tells you if you saved money or lost it depending on what you had projected to spend.
      If projectedSavings <= totalSavings Then
        TextWindow.WriteLine(" you saved money!")
      EndIf  
      If  projectedSavings > totalSavings then 
        TextWindow.WriteLine(" you lost money, sorry")
        
      EndIf
      
      
      
      TextWindow.WriteLine("Do you want to do it again? (y/n)")
      input = TextWindow.Read() ' get input
      input = Text.GetSubText(input,1,1) ' just get first character
      input = Text.ConvertToLowerCase(input) ' convert to lower case y
      If (input <> "y") Then ' anythng apart from something starting with y or Y will end the While loop
        doContinue = "False"
      EndIf
    EndWhile


    thank you so much. it worked out just right and now i just need help validating my values.  3. Make sure the user enters numeric values of zero or greater when prompted. If the user enters invalid data, he or she must be alerted and asked again repeatedly until valid data is supplied.  

    im stuck on how to do this part

    Sunday, October 18, 2015 6:01 PM
  • its just the same idea, while invalid data repeat

    dataInvalid = "True"
    While (dataInvalid)
      TextWindow.WriteLine("Enter a nmber > 0")
      input = TextWindow.ReadNumber()
      If (input > 0) Then
        dataInvalid = "False"
      Else
        TextWindow.WriteLine("please do it right")
      EndIf
    EndWhile
    You should be able to use this idea for all input, checking for whatever the valid data is.

    Sunday, October 18, 2015 6:20 PM
    Moderator
  • its just the same idea, while invalid data repeat

    dataInvalid = "True"
    While (dataInvalid)
      TextWindow.WriteLine("Enter a nmber > 0")
      input = TextWindow.ReadNumber()
      If (input > 0) Then
        dataInvalid = "False"
      Else
        TextWindow.WriteLine("please do it right")
      EndIf
    EndWhile
    You should be able to use this idea for all input, checking for whatever the valid data is.

    this is what i tried but it didnt work:

    TextWindow.WriteLine("Enter the bills without garden.")
    For i = 1 To 12
      TextWindow.Write(monthsInYear[i] + ": ")
      withoutGarden[i] = TextWindow.ReadNumber()
      totalWithoutGarden = totalWithoutGarden + withoutGarden[i]
    EndFor
    
    If WithoutGarden[i] < 0 Then 
      TextWindow.Writeline(" Error! The value must be greater than 0")
      TextWindow.Writeline(" Enter correct value: ")
      WithoutGarden[i] = TextWindow.ReadNumber()
    EndIf 
    
    'get bills with garden
    TextWindow.WriteLine("Enter the bills with garden.")
    For i = 1 to 12
       TextWindow.Write(monthsInYear[i] + ": ")
       withGarden[i] = TextWindow.ReadNumber()
       totalWithGarden = totalWithGarden + withGarden[i]
    EndFor
    If WithGarden[i] < 0 Then 
      TextWindow.Writeline(" Error! The value must be greater than 0")
      TextWindow.Writeline(" Enter correct value: ")
      WithGarden[i] = TextWindow.ReadNumber()
    EndIf 
    
    'calculate savings
    For i = 1 to 12
      savings[i] = withoutGarden[i] - withGarden[i]
      totalSavings = totalSavings + savings[i]
    EndFor
    If savings[i] < 0 Then 
      TextWindow.Writeline(" Error! The value must be greater than 0")
      TextWindow.Writeline(" Enter correct value: ")
      savings[i] = TextWindow.ReadNumber()
    EndIf 

    Sunday, October 18, 2015 6:22 PM
  • its just the same idea, while invalid data repeat

    dataInvalid = "True"
    While (dataInvalid)
      TextWindow.WriteLine("Enter a nmber > 0")
      input = TextWindow.ReadNumber()
      If (input > 0) Then
        dataInvalid = "False"
      Else
        TextWindow.WriteLine("please do it right")
      EndIf
    EndWhile
    You should be able to use this idea for all input, checking for whatever the valid data is.

    if i try your way where would i put it in my program?

    Sunday, October 18, 2015 6:23 PM
  • The if method you use is Ok but it only checks one error - a while will catch multiple errors until the user gets it right

    For i = 1 to 12 TextWindow.Write(monthsInYear[i] + ": ") withGarden[i] = TextWindow.ReadNumber() totalWithGarden = totalWithGarden + withGarden[i] EndFor If WithGarden[i] < 0 Then TextWindow.Writeline(" Error! The value must be greater than 0") TextWindow.Writeline(" Enter correct value: ") WithGarden[i] = TextWindow.ReadNumber() EndIf

    The If should be inside the For loop since you wanto check each entry  If you prefer to use GoTo (While is considered better programing though)

    For i = 1 to 12
      TextWindow.Write(monthsInYear[i] + ": ")
      repeatWithGarden:
      withGarden[i] = TextWindow.ReadNumber()
      If WithGarden[i] < 0 Then 
        TextWindow.Writeline(" Error! The value must be greater than 0")
        TextWindow.Writeline(" Enter correct value: ")
        GoTo repeatWithGarden
      EndIf 
      totalWithGarden = totalWithGarden + withGarden[i]
    EndFor

    Sunday, October 18, 2015 6:34 PM
    Moderator
  • The if method you use is Ok but it only checks one error - a while will catch multiple errors until the user gets it right

    For i = 1 to 12 TextWindow.Write(monthsInYear[i] + ": ") withGarden[i] = TextWindow.ReadNumber() totalWithGarden = totalWithGarden + withGarden[i] EndFor If WithGarden[i] < 0 Then TextWindow.Writeline(" Error! The value must be greater than 0") TextWindow.Writeline(" Enter correct value: ") WithGarden[i] = TextWindow.ReadNumber() EndIf

    The If should be inside the For loop since you wanto check each entry  If you prefer to use GoTo (While is considered better programing though)

    For i = 1 to 12
      TextWindow.Write(monthsInYear[i] + ": ")
      repeatWithGarden:
      withGarden[i] = TextWindow.ReadNumber()
      If WithGarden[i] < 0 Then 
        TextWindow.Writeline(" Error! The value must be greater than 0")
        TextWindow.Writeline(" Enter correct value: ")
        GoTo repeatWithGarden
      EndIf 
      totalWithGarden = totalWithGarden + withGarden[i]
    EndFor


    thank you so much for all your help. Do you by chance have an email address i could have to email you if i have problems on future assignments? Your so much help to me and are a better teacher than my own teacher. thanks again
    Sunday, October 18, 2015 6:41 PM
  • no this forum is where me and others help out - the  reason we do it here is that the questions, answers and discussion can help more than just the asker and provide a back history of questions and replies that can be searched.

    good luck.

    Sunday, October 18, 2015 6:47 PM
    Moderator