QBASIC

Associate
Joined
29 Jul 2005
Posts
445
Location
Matlock
Hi there, I have started using QBASIC at school and we've been set the task to complete a quiz on captial citys. I have completed this task however I am a bit of a perfectionist and just wondered if there was a way of making the random number generator NOT generate a number that has been previously, thus not asking the same question twice

Here is what I have so far:

Code:
REM Capital City
DIM country$(20), capital$(20)
RANDOMIZE TIMER
CLS
LET q = 1
LET score = 0
        FOR x = 1 TO 13
            READ country$(x), capital$(x)
        NEXT x

INPUT "How many questions do you want"; qnum

FOR q = 1 TO qnum STEP 1
        nub = INT(RND * 13) + 1
        PRINT "What is the capital of "; country$(nub); "?"
        INPUT answer$
        IF answer$ = capital$(nub) THEN
            LET score = score + 1
            PRINT "Well done, that was the correct answer"
            PRINT " "
            ELSE
            PRINT "Sorry, the capital of "; country$(nub); " is "; capital$(nub)
            PRINT " "
        END IF
NEXT q
total = score / qnum
COLOR 9
PRINT "Your score is "; score; "/"; qnum

END

DATA France, Paris, Albania, Tirona, Finland, Helsinki, Ireland, Dublin
DATA England, London, Austria, Vienna, Germany, Berlin, Luxembourg, Luxembourg
DATA Barbados, Bridgetown, USA, Washington DC, Russia, Moscow,
DATA Eygpt, Cairo, Greece, Athens,

I'm really stumped now and have been trying to do this for ages!! Any help would be appreciatted

thanks :)

marc
 
Code:
REM Capital City
DIM country$(20), capital$(20)
RANDOMIZE TIMER
CLS
q = 1
score = 0
        FOR x = 1 TO 13
            READ country$(x), capital$(x)
        NEXT x

INPUT "How many questions do you want"; qnum

FOR q = 1 TO qnum
        nub = INT(RND * 13) + 1
        PRINT "What is the capital of "; country$(nub);
        INPUT answer$
        IF answer$ = capital$(nub) THEN
            score = score + 1
            PRINT "Well done, that was the correct answer"
            PRINT 
        ELSE
            PRINT "Sorry, the capital of "; country$(nub); " is "; capital$(nub)
            PRINT 
        END IF
NEXT q

total = score / qnum

COLOR 9
PRINT "Your score is "; score; "/"; qnum

END

DATA France, Paris, Albania, Tirona, Finland, Helsinki, Ireland, Dublin
DATA England, London, Austria, Vienna, Germany, Berlin, Luxembourg, Luxembourg
DATA Barbados, Bridgetown, USA, Washington DC, Russia, Moscow,
DATA Eygpt, Cairo, Greece, Athens,
Took some code out, and made the overall file size slightly smaller. :o
 
ok thanks basmic, i'm pretty new to QBASIC so most that code was left in there just because i was unsure wether it was needed or not

any ideas on how to prevent duplicate random numbers though?? :)
 
I'm no QBASIC expert so there might be a better way of doing this. In fact there probably is....

One way of solving your problem would be to use an array to hold indicators for all your already chosen values. When a random number is generated (nub) check to see if that element in your array is populated. If not, set it to X and use the number. If it is already populated pick another number.

Some pseudo-code below:

dim usednumber$(20)
gotnumber = 0

/* This would go within your main loop. */

while gotnumber = 0
nub = int(RND*13)+1

if usednumber$(nub) !="X"
then
/* We can use this number so mark it as used. */
gotnumber = 1
usednumber$(nub)= "X"
end if
wend

It's not ideal, as more numbers are used it could take some time to find unused numbers.

Jim
 
Oh right thanks for that idea, I was thinking about trying to include an extra array however wasn't too sure how to do it.

Will give it a bash 2moro and see how i get on
 
Back
Top Bottom