Home > Basic Programming > QBasic Tutorial 21-Preventing Duplicate Data In The Same Array

QBasic Tutorial 21-Preventing Duplicate Data In The Same Array

QBasic Tutorial 21 – Preventing Duplicate Data In The Same Arrray

 

Make a program that will hold 4 First names within an array.

 

So first we declare FN which is First Names as a String:

DIM FN AS STRING

Declare an array called FNames, there will be four first names in this array. Created

as a string.

DIM FNames(4) AS STRING

We need a two counter-variables for a couple of FOR LOOP’s:

So we declare x as an integer as well as y:

DIM x AS INTEGER DIM y AS INTEGER

 

Then we declare Found as an Integer:

DIM Found AS INTEGER

Then clear the screen:

CLS

 

So here are declare statements:

DIM FN AS STRING DIM FNames(4) AS STRING DIM x AS INTEGER DIM y AS INTEGER DIM Found AS INTEGER

CLS

Start the FOR LOOP:

FOR x = 1 TO 4

Next start the DO LOOP:

FOR x = 1 TO 4  DO   INPUT “Enter Name: “, FN      —Placing the user input value within

FN.

Then set Found to equal 0 after the user input enter name:

INPUT “Enter Name: “, FN Found = 0

FOR x = 1 TO 4  DO   INPUT “Enter Name: “, FN   Found = 0     FOR y = 1 TO 4    IF UCASE$(FN) = UCASE$(FNames(y)) THEN

 

–This statement above means if the uppercase of FN is the same as the uppercase

conversion UCASE FNames y THEN we know that the name has been found.

So set Found to equal 1:

FOR y = 1 TO 4  IF UCASE$(FN) = UCASE$(FNames(y)) THEN   Found = 1  END IF NEXT y

IF Found = 1 THEN   PRINT “Duplicate Names Are Not Accepted”  END IF LOOP UNTIL Found = 0

FNames(x) = FN NEXT x

 

 

So here is what’s going to happen:

We start the FOR LOOP at FOR x = 1 TO 4. Then we start the DO LOOP until Found =

False or also 0.

We’ll get the user input and place it within FN, set Found to equal false or 0.

Within the second FOR LOOP we will check to see if the user input is the same as

what’s already in the array. If it is then Found becomes 1 or True.

Then we will go ahead and print duplicate names are not accepted loop back to prompt

the user to enter a name again.

If the name is not found the LOOP ends, then we’re alound to place our name within

our name array, then we go to our next x, and then repeating the process until our

loop ends.

The full code so far:

 

DIM FN AS STRING DIM FNames(4) AS STRING DIM x AS INTEGER DIM y AS INTEGER DIM Found AS INTEGER

CLS

FOR x = 1 TO 4  DO   INPUT “Enter Name: “, FN   Found = 0

FOR y = 1 TO 4    IF UCASE$(FN) = UCASE$(FNames(y)) THEN     Found = 1    END IF   NEXT y      IF Found = 1 THEN    PRINT “Duplicate Names Are Not Accepted”   END IF  LOOP UNTIL Found = 0    FNames(x) = FN NEXT x

 

Next we will print out the information: Printing out the entire array

PRINT PRINT PRINT “The Names In The Array: ”

FOR x = 1 TO 4  PRINT FNames(x) NEXT x

 

Ok so now we go ahead and run the code.

So this code is accepting 4 names for an array and will not accept duplicate names.

Final Code:

 

 

DIM FN AS STRING DIM FNames(4) AS STRING DIM x AS INTEGER DIM y AS INTEGER DIM Found AS INTEGER

CLS

FOR x = 1 TO 4  DO   INPUT “Enter Name: “, FN   Found = 0

FOR y = 1 TO 4    IF UCASE$(FN) = UCASE$(FNames(y)) THEN     Found = 1    END IF   NEXT y      IF Found = 1 THEN    PRINT “Duplicate Names Are Not Accepted”   END IF  LOOP UNTIL Found = 0    FNames(x) = FN NEXT x

PRINT PRINT PRINT “The Names In The Array: ”

FOR x = 1 TO 4  PRINT FNames(x) NEXT x

 

 

 

 

 

 

Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: