Simple QBASIC Question

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
Austin
Coder
Posts: 36
Joined: Wed Aug 01, 2007 6:09 am
Location: Colorado
Contact:

Simple QBASIC Question

Post 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
Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Try this

Post 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
User avatar
Austin
Coder
Posts: 36
Joined: Wed Aug 01, 2007 6:09 am
Location: Colorado
Contact:

Post 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
Mac
Veteran
Posts: 151
Joined: Mon Aug 06, 2007 2:00 pm

Post 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
User avatar
Austin
Coder
Posts: 36
Joined: Wed Aug 01, 2007 6:09 am
Location: Colorado
Contact:

Post by Austin »

Ah, ELSEIF command.

Thanks so much!
Austin
User avatar
Theophage
Coder
Posts: 44
Joined: Sun May 07, 2006 7:32 pm
Location: Tucson, AZ
Contact:

Post 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
Daniel "Theophage" Clark
theophage (at) geocities (dot) com

"God used to be my co-pilot, but our plane crashed in the mountains and I had to eat Him..."
User avatar
Austin
Coder
Posts: 36
Joined: Wed Aug 01, 2007 6:09 am
Location: Colorado
Contact:

Post 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
Post Reply