idril
New Member
Posts: 15
|
Post by idril on Aug 18, 2006 9:03:37 GMT
I do not understand how to get data of a file into an array.. But I did change some code. It is shorter now.(And instead of GOTO used GOSUB. Is that ok?)
CLS file$ = "NAMES.TXT" ' make space for 500 names DIM Names$(500) ' see if file exist or else make the file OPEN file$ FOR APPEND AS #1 CLOSE #1 ' enter a name.. DO INPUT "Enter name (q to quit): ", Name$ ' makes it all uppercase Name$ = LTRIM$(RTRIM$(UCASE$(Name$))) IF Name$ = "Q" THEN EXIT DO FOR l = 1 TO el IF Name$ = Names$(l) THEN EXIT FOR NEXT l IF l <= el THEN PRINT Name$; " found at line number"; l ELSE PRINT Name$; " is not found! The name is now added." GOSUB Newname END IF INPUT "Add another name? (y/n) ", add$ IF add$ = "y" OR add$ = "Y" THEN GOSUB Putinfile IF add$ = "n" OR add$ = "N" THEN END IF add$ = "" THEN END LOOP CLOSE END Putinfile: ' write in file OPEN file$ FOR INPUT AS #1 el = 0 WHILE NOT EOF(1) el = el + 1 LINE INPUT #1, Names$(el) WEND CLOSE RETURN Newname: ' add a name OPEN file$ FOR APPEND AS #1 PRINT #1, Name$ CLOSE GOSUB Putinfile RETURN
|
|
|
Post by Mikrondel on Aug 18, 2006 10:26:55 GMT
Well, if you're having trouble, start with something simpler. (Your GOSUB looks good, by the way)
So try this:
Start with DIM Names$(1 TO 20)
This reserves some space for names. Now, the number of names you actually store in it can be between 0 and 20. Clearly the program should know how many there are, otherwise it'd have trouble working with them. The simplest thing to do is to store all the names consecutively (one after another, i.e. in Names$(1), then Names$(2), then Names$(3), ...) and store the position of the last name, using a variable like "NameIndex".
Now, ask the user to enter a name. Search the array for the name (i.e. go through all the array's used elements {using a FOR-loop; this only needs to check used names, i.e. only check names up to NameIndex} and see if any of them are the same as the entered name) and if it does not appear, then add it to the array. (When you add it to the array, you must place it AFTER the current last name, and increase your name index by 1 to indicate the number of names stored being 1 more.) If the name DOES appear then simply tell the user of this.
Then ask the user for another name, etc.
Note this has absolutely no file access to confuse anything. Do not use OPEN. Don't worry about sorting. Just adding names to an array one by one, and also searching the current list of names for a certain name.
If you have any problems, spend some time on them yourself and if still stuck then come and ask us.
Good luck!
|
|
|
Post by Kelserm on Oct 26, 2019 9:14:55 GMT
Propecia Buy Cheap Apres Clomid Fertilite Propecia After 10 Years Testosterone Levels viagra Amoxil Dosage Pediatric Azithromycin Cost 500mg China Comprar Cialis Andalucia
|
|
|
Post by Kelserm on Nov 4, 2019 15:45:51 GMT
|
|