qbasic-What is wrong with my code?I'm not getting what i want

213 views Asked by At

I tried to make a simple login program using qbasic. I used 3 sub modules and 1 main module. Here is the code:

DECLARE SUB login ()
DECLARE SUB menu ()
DECLARE SUB REGISTER ()
CLS
CALL menu
END

SUB login
OPEN "USERPASS.TXT" FOR INPUT AS #2
CLS
97
LOCATE 2, 30: PRINT "LOGIN"
LOCATE 4, 10: INPUT "PLEASE ENTER CORRECT USERNAME OR PRESS B IF YOU WANT TO GO BACK"; USER$
IF UCASE$(USER$) = "B" THEN
    CLOSE #2
    CALL menu
ELSE
    DO WHILE NOT EOF(2)
        INPUT #2, U$, p$
        IF U$ = USER$ THEN
            TEMPUSER$ = U$
            PASSWORDS$ = p$
            EXIT DO
        END IF
    LOOP
END IF
IF USER$ = TEMPUSER$ THEN
98
    PRINT PASSWORDS$
    LOCATE 5, 30: INPUT "ENTER PASSWORD OR PRESS B if you want to go back"; password$
    IF UCASE$(password$) = "B" THEN 
        GOTO 97
    ELSE
        IF password$ = PASSWORDS$ THEN
            PRINT "HURRAY YOU LOGGED IN"           
        ELSE
            GOTO 98
        END IF
    END IF
ELSE
    GOTO 97
END IF
CLOSE #2
END SUB

SUB menu
CLS
23
LOCATE 2, 30: PRINT "MAIN MENU"
LOCATE 4, 30: PRINT "1.LOGIN"
LOCATE 5, 30: PRINT "2.REGISTER"
LOCATE 6, 30: INPUT "PLEASE! Enter 1 or 2"; a
IF a = 1 THEN
    CALL login
ELSE
    IF a = 2 THEN
        CALL REGISTER
    ELSE
        CLS
        GOTO 23
    END IF
END IF
END SUB

SUB REGISTER
CLS
OPEN "userpass.txt" FOR APPEND AS #1
LOCATE 2, 30: PRINT "REGISTER MENU"
LOCATE 4, 30: INPUT "ENTER NEW USERNAME"; NEWU$
CLS
LOCATE 4, 30: INPUT "ENTER NEW PASSWORD"; PASSU$
WRITE #1, NEWU$, PASSU$
END SUB

Everything is as my desire but in sub module login something is wrong I guess. The login massage does not come even if I enter correct password and username. But if I enter username which does not exist, it shows the login message. Please help. Thanks in advance.

1

There are 1 answers

1
AudioBubble On BEST ANSWER

You're missing an END IF to match ELSEIF UCASE$(USER$) <> "B" THEN (by the way: if UCASE$(USER$) = "B" is false, then UCASE$(USER$) <> "B" is true, so you could simply say ELSE there).

You might try indenting in a bit more conventional manner:

  • every IF, ELSEIF, ELSE, and END IF that matches is in the same column, and
  • everything inside those statements is indented a few more spaces.

The same indentation rules can be applied to SELECT CASE, WHILE-WEND, DO-LOOP, and FOR-NEXT.

Anyway, you might see the missing END IF if you did that:

SUB login
    OPEN "USERPASS.TXT" FOR INPUT AS #2
    CLS
    97
    LOCATE 2, 30: PRINT "LOGIN"
    LOCATE 4, 10: INPUT "PLEASE ENTER CORRECT USERNAME OR PRESS B IF YOU WANT TO GO BACK"; USER$
    IF UCASE$(USER$) = "B" THEN
        CLOSE #2
        CALL menu
    ELSEIF UCASE$(USER$) <> "B" THEN
        WHILE NOT EOF(2)
            INPUT #2, U$, p$
            IF U$ = USER$ THEN
                TEMPUSER$ = U$
                PASSWORDS$ = p$
            END IF
        WEND
        IF USER$ = TEMPUSER$ THEN
            98
            PRINT PASSWORDS$
            LOCATE 5, 30: INPUT "ENTER PASSWORD OR PRESS B if you want to go back"; password$
            IF UCASE$(password$) = "B" THEN GOTO 97
            ELSEIF UCASE$(password$) <> "B" THEN
                IF password$ = PASSWORDS$ THEN
                    PRINT "HURRAY YOU LOGGED IN"
                ELSE
                    GOTO 98
                END IF
            END IF
        ELSE
            GOTO 97
        END IF

        CLOSE #2
END SUB