14

I have an IF statement in QBASIC... yes... QBASIC...

I have been teaching someone to program (I decided this would be nice and easy to see how the syntax works).

...Anyway, I have this code:

CLS

start:
INPUT ">>", a$

PRINT a$
IF (INSTR(a$, "do you")) THEN
    IF (INSTR(a$, "like")) THEN
        IF (INSTR(a$, "cheese")) THEN PRINT "Yep, I like cheese":
        IF (INSTR(a$, "music")) THEN PRINT "Depends, which genre?": GOTO musicGenre
    ELSE IF (INSTR(a$, "hate")) THEN
            IF (INSTR(a$, "cheese")) THEN PRINT "No, I like cheese"
        END IF
    END IF
END IF


musicGenre:
INPUT ">>", m$
SELECT CASE (m$)
    CASE "pop"
        PRINT "..pop! lol, baa baa"
    CASE "rock"
        PRINT "Rock is ok"
END SELECT
GOTO start

But when I type "do you like cheese?" it seems to only reply "Yep, I like cheese" every other time...

Could anyone shed some light on this?

note:
"do you like music?" works every time...

note 2:
Screenshot of the output:
alt text

Barrie Reader
  • 10,647
  • 11
  • 71
  • 139

3 Answers3

4

Your code you provided appears correct.

Try one of the following:

  • If possible, send us a larger code sample. I'm guessing the error is outside the code you provided.
  • Output the input (a$) before the first IF to confirm your code will be working with the expected input.
  • In most languages, FALSE is zero and true is anything else. However, you may want to be more explicit with the following IF (INSTR(a$) > 0).

EDIT: You should put a goto start on any cheese result. Otherwise, it's going to the musicGenre code.

Steven
  • 13,501
  • 27
  • 102
  • 146
  • 1
    Hey, cheers very much. a$ outputs as "`do you like cheese`" **however**, it won't even output the a$ afterwards.. like.. the output is right the first time, then it is empty (including the outputted a$) the second time, then it's correct the third. – Barrie Reader Sep 16 '10 at 13:21
2
CLS

start:
    INPUT ">>", a$

    IF (INSTR(1, a$, "do you")) THEN
        IF (INSTR(1, a$, "like")) THEN
            IF (INSTR(1, a$, "cheese")) THEN PRINT "Yep, I like cheese"
            IF (INSTR(1, a$, "music")) THEN PRINT "Depends, which genre?": GOSUB musicGenre
        END IF
    IF (INSTR(1, a$, "hate")) THEN
        IF (INSTR(1, a$, "cheese")) THEN PRINT "No, I like cheese"
    END IF
END IF

GOTO start
musicGenre:
    INPUT ">>", m$
    SELECT CASE (m$)
        CASE "pop"
            PRINT "..pop! lol, baa baa"
        CASE "rock"
            PRINT "Rock is ok"
    END SELECT
RETURN
Wookie88
  • 33,079
  • 4
  • 27
  • 32
Michael
  • 21
  • 1
  • 2
    Code without any explanation isn't an acceptable answer here. Please [edit] to provide some information about **how** your code solves the problem. Thanks. – Ken White Jul 06 '13 at 22:39
0

This program demonstrates parsing input and gosubs in Basic.

REM Cheese progran source:
CLS
DO
    INPUT ">>", a$
    a$ = LCASE$(a$)
    PRINT a$
    IF INSTR(a$, "do you") THEN
        IF INSTR(a$, "like") THEN
            IF INSTR(a$, "cheese") THEN
                PRINT "Yep, I like cheese":
            END IF
            IF INSTR(a$, "music") THEN
                PRINT "Depends, which genre?"
                GOSUB MusicGenre
            END IF
        ELSE
            IF INSTR(a$, "hate") THEN
                IF INSTR(a$, "cheese") THEN
                    PRINT "No, I like cheese"
                END IF
            END IF
        END IF
    END IF
LOOP
END
MusicGenre:
INPUT ">>>", m$
a$ = LCASE$(a$)
SELECT CASE (m$)
    CASE "pop"
        PRINT "..pop! lol, baa baa"
    CASE "rock"
        PRINT "Rock is ok"
END SELECT
RETURN
eoredson
  • 1,167
  • 2
  • 14
  • 29