Page 1 of 1

Simple QBASIC Question

Posted: Wed Oct 17, 2007 2:38 pm
by Austin
Ok heres the code:

Code: Select all

PRINT "Select a gender:"
PRINT "1. Male"
PRINT "2. Female"
GENDER:
INPUT "> ", G$
IF G$ = "1" THEN
PRINT
PRINT "1, Selected."
ELSE PRINT "Please select 1 or 2."
GOTO GENDER
END IF
IF G$ = "2" THEN
PRINT
PRINT "2, Selected."
ELSE PRINT "Please select 1 or 2."
GOTO GENDER
END IF
The problem I have as that when I select 1 it conflicts with 2 and says "Please select 1 or 2." and If I select 2 it conflicts with 1 and says "Please select 1 or 2." I'm sure there is an easy fix to this, I just have forgot been away from the language too long.

Regards,
Austin

Try this

Posted: Wed Oct 17, 2007 3:13 pm
by Mac

Code: Select all

' First get a valid choice from the user
PRINT "Select a gender:"
PRINT "1. Male"
PRINT "2. Female"
DO
  INPUT "> ", G$
  IF G$ <> "1" AND G$ <> "2" THEN PRINT "Please select 1 or 2."
LOOP WHILE G$ <> "1" AND G$ <> "2"

' Then process the valid choice
IF G$ = "1" THEN
  PRINT "1, Selected."
ELSE
  PRINT "2, Selected."
END IF

Posted: Wed Oct 17, 2007 6:47 pm
by Austin
Hmm, that works but seems to only work if there were 2 choices. How would you write it if you had 4 options:
1. Male
2. Female
3. Cyborg
4. None

Thanks for the reply,
-Austin

Posted: Thu Oct 18, 2007 9:26 am
by Mac
Austin wrote:Hmm, that works but seems to only work if there were 2 choices. How would you write it if you had 4 options

Code: Select all

' First get a valid choice from the user
PRINT "Select a gender:"
PRINT "1. Male"
PRINT "2. Female"
PRINT "3. Cyborg"
PRINT "4. None "

DO
  INPUT "> ", G$
  IF G$ <> "1" AND G$ <> "2" AND G$ <> "3" AND G$ <> "4" THEN
    PRINT "Please select 1, 2, 3 or 4."
  END IF
LOOP WHILE G$ <> "1" AND G$ <> "2" AND G$ <> "3" AND G$ <> "4"

' Then process the valid choice
IF G$ = "1" THEN
  PRINT "1, Selected."
ELSEIF G$ = "2" THEN
  PRINT "2, Selected."
ELSEIF G$ = "3" THEN
  PRINT "3, Selected."
ELSE
  PRINT "4, Selected."
END IF

Posted: Thu Oct 18, 2007 10:55 pm
by Austin
Ah, ELSEIF command.

Thanks so much!
Austin

Posted: Fri Oct 19, 2007 9:47 am
by Theophage
Another method would be to use the SELECT CASE command like so:

Code: Select all

' Then process the valid choice
SELECT CASE G$
  CASE = "1": PRINT "1, Selected."
  CASE = "2": PRINT "2, Selected."
  CASE = "3": PRINT "3, Selected."
  CASE = "4": PRINT "4, Selected."
END SELECT

Posted: Fri Oct 19, 2007 3:51 pm
by Austin
Wow alright, the CASE is a lot easier to use and a lot less typing. Also, good to know is when none of the cases is met to use
CASE ELSE PRINT "Please choose 1, 2, 3 or 4."
END SELECT

Thanks for the Reply!
Austin