locked
Nested While Lopp (small basic) makes average of numbers put in. RRS feed

  • Question

  • total=0
    count=0
    TextWindow.Write("Enter a student name: ")
    name= TextWindow.Read()

    While name <> ""
      TextWindow.Write("Enter the students grades: " )
      grades = TextWindow.ReadNumber()     'The program stops here for some reason when I run it.
      While grades <> ""
        total= total+grades
        count= count+1

    EndWhile
      TextWindow.Write(name+"'s average is " +total/grades)
      TextWindow.Write("Enter another students name or hit enter to quit: ")
      answer= TextWindow.Read()
     Endwhile

    I know the problem is in the second loop when I run the program it stops when I enter one grade.

        
    Saturday, November 9, 2013 10:54 AM

Answers

  • name cannot change after it is input, so what is the purpose of the While name <> "" loop?

    grades = TextWindow.ReadNumber() will only return when a number is entered correctly, so While grades <> "" must be true and always be true so its contents just keeps running forever.

    Some TextWindow debug statements helps to see the effect.

    total=0
    count=0
    TextWindow.Write("Enter a student name: ")
    name= TextWindow.Read()
    
    While name <> ""
      TextWindow.WriteLine("Name While Loop")
      TextWindow.Write("Enter the students grades: " )
      grades = TextWindow.ReadNumber()     'The program stops here for some reason when I run it.
      While grades <> ""
        total= total+grades
        count= count+1
        TextWindow.WriteLine("Grades While Loop")
      EndWhile
      TextWindow.Write(name+"'s average is " +total/grades)
      TextWindow.Write("Enter another students name or hit enter to quit: ")
      answer= TextWindow.Read()
    Endwhile
    Perhaps you want If or at least reconsider what the While loops are doing.

    • Proposed as answer by Nonki Takahashi Monday, November 11, 2013 1:01 PM
    • Marked as answer by litdev Monday, November 18, 2013 9:57 PM
    Saturday, November 9, 2013 5:19 PM
  • At first TextWindow.ReadNumber() returns 0 if just hit enter.  Try this code.

    While "True"
      TextWindow.Write("number? ")
      num = TextWindow.ReadNumber()
      TextWindow.WriteLine("number = " + num)
    EndWhile

    One solution is to use TextWindow.Read().  Following code is a sample.

    total=0
    count=0
    TextWindow.Write("Enter a student name: ")
    name= TextWindow.Read()
    While name <> ""
      TextWindow.Write("Enter the students grades: " )
      grades = TextWindow.Read()
      While grades <> ""
        total= total+grades
        count= count+1
        TextWindow.Write("Enter the students grades: " )
        grades = TextWindow.Read()
      EndWhile
      TextWindow.WriteLine(name+"'s average is " +total/count)
      total=0
      count=0
      TextWindow.Write("Enter another students name or hit enter to quit: ")
      name = TextWindow.Read()
    EndWhile


    Nonki Takahashi

    • Proposed as answer by Nonki Takahashi Monday, November 11, 2013 1:01 PM
    • Marked as answer by litdev Monday, November 18, 2013 9:57 PM
    Monday, November 11, 2013 1:00 PM

All replies

  • name cannot change after it is input, so what is the purpose of the While name <> "" loop?

    grades = TextWindow.ReadNumber() will only return when a number is entered correctly, so While grades <> "" must be true and always be true so its contents just keeps running forever.

    Some TextWindow debug statements helps to see the effect.

    total=0
    count=0
    TextWindow.Write("Enter a student name: ")
    name= TextWindow.Read()
    
    While name <> ""
      TextWindow.WriteLine("Name While Loop")
      TextWindow.Write("Enter the students grades: " )
      grades = TextWindow.ReadNumber()     'The program stops here for some reason when I run it.
      While grades <> ""
        total= total+grades
        count= count+1
        TextWindow.WriteLine("Grades While Loop")
      EndWhile
      TextWindow.Write(name+"'s average is " +total/grades)
      TextWindow.Write("Enter another students name or hit enter to quit: ")
      answer= TextWindow.Read()
    Endwhile
    Perhaps you want If or at least reconsider what the While loops are doing.

    • Proposed as answer by Nonki Takahashi Monday, November 11, 2013 1:01 PM
    • Marked as answer by litdev Monday, November 18, 2013 9:57 PM
    Saturday, November 9, 2013 5:19 PM
  • At first TextWindow.ReadNumber() returns 0 if just hit enter.  Try this code.

    While "True"
      TextWindow.Write("number? ")
      num = TextWindow.ReadNumber()
      TextWindow.WriteLine("number = " + num)
    EndWhile

    One solution is to use TextWindow.Read().  Following code is a sample.

    total=0
    count=0
    TextWindow.Write("Enter a student name: ")
    name= TextWindow.Read()
    While name <> ""
      TextWindow.Write("Enter the students grades: " )
      grades = TextWindow.Read()
      While grades <> ""
        total= total+grades
        count= count+1
        TextWindow.Write("Enter the students grades: " )
        grades = TextWindow.Read()
      EndWhile
      TextWindow.WriteLine(name+"'s average is " +total/count)
      total=0
      count=0
      TextWindow.Write("Enter another students name or hit enter to quit: ")
      name = TextWindow.Read()
    EndWhile


    Nonki Takahashi

    • Proposed as answer by Nonki Takahashi Monday, November 11, 2013 1:01 PM
    • Marked as answer by litdev Monday, November 18, 2013 9:57 PM
    Monday, November 11, 2013 1:00 PM