qbasic

How do I fix this problem with While and For loop?


I'm writing a program in QBasic that ask for peoples names and ages infinite times until I press xxx and the program then prints all those entered names and ages:

I keep getting Zeros instead of the names and ages, no matter how hard I try, I am totally stuck, below is my code:

DIM i AS INTEGER
CLS
i = 1
WHILE nam$ <> "xxx"
   Input "Enter name: ", nam$
   Input "Enter age: ", age$
   Sum$ = nam$ + age$ (i)
   i = i + 1
WEND
FOR x = 1 to i
    PRINT Sum(i)
NEXT x

Solution

  • Same code only using while:

    REM how to enter data and add to array using only while
    DIM i AS INTEGER
    CLS
    DIM Sum(16384) AS STRING
    PRINT "enter xxx to quit."
    WHILE z = 0
        INPUT "Enter name: ", nam$
        INPUT "Enter age: ", age$
        IF nam$ = "xxx" THEN
            z = -1
        ELSE
            i = i + 1
            Sum(i) = nam$ + " " + age$
        END IF
    WEND
    FOR x = 1 TO i
        PRINT Sum(x)
    NEXT x