Qbasicnews.com

Full Version: Three Questions
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
How can the user input an order, so that certain strings will be put in a certain order. (Like strin1$ = s$ + a$ + u$, but the user is inputted to do this) This is kinda hard to explain, so I'll give an example:
Code:
music1$ = "adadad"  'enter 1 for this part
music2$ = "becebece" 'enter 2 for this part
music3$ = "adfadfadf" 'enter 3 for this part
music4$ = "acedcafg" 'enter 4 for this part
INPUT "enter order of riffs ", order$
c = LEN(order$)
FOR i = 1 TO c
order1 = ASC(MID$(order$, 1, c))
IF order1 = 49 THEN fullcompo$ = fullcompo$ + music1$
IF order1 = 50 THEN fullcompo$ = fullcompo$ + music2$
IF order1 = 51 THEN fullcompo$ = fullcompo$ + music3$
IF order1 = 52 THEN fullcompo$ = fullcompo$ + music4$
NEXT
PRINT fullcompo$
PLAY fullcompo$

But once I input an order, and I do the for to next loop, it only PRINTs and PLAYs the first part of fullcompo$. Any help is appreciated.

Second Question:

How do I delete the first line in a sequential file and then overwrite it with something else?

Third Question:

Is there a way you can print specifically what is causing an error in a program? If not, it's okay I'll use something else.

Thanks for any help Smile Smile Smile Smile
For the moment, I can answer the third one:
ERR and ERL return the error number and error line where an error occured, respectivly.
Oh, well thank you for the reply, but i need to know more specifically then that Wink ERR returns the error number, and ERL returns the line number. Thanks though!
You just need to make a few changes to your code to get it to do what you want. First change the ASC to VAL, as you need the actual value not the ascii character code. Secondly you need to make the FOR step through two at a time by chaning it to FOR i = 1 TO c STEP 2. Lastly you just need to change the MID$ part to MID$(order$, i, 2) as it currently just reads from the same starting point to the end(i.e. its not changing from loop to loop.

Regarding the error codes, the easiest way is to get a list of qb error codes and do a manual check on each code and print your own error statement(or what is listed). I don't think there is an easier way, unless you find a set of routines already done.
First question:
[syntax="qbasic"]
DIM Musica(1 TO 4) AS STRING
Musica(1) = "adadad" 'enter 1 for this part
Musica(2) = "becebece" 'enter 2 for this part
Musica(3) = "adfadfadf" 'enter 3 for this part
Musica(4) = "acedcafg" 'enter 4 for this part
INPUT "Order -> ", order$

c = LEN(order$)
FOR i = 1 TO c
order1 = VAL(MID$(order$, c, 1))
fullcompo$ = fullcompo$ + Musica(order1)
NEXT i
PRINT fullcompo$
PLAY "X" + VARPTR$(fullcompo$)
[/syntax]
Should work now.

Second Question:
Quote:How do I delete the first line in a sequential file and then overwrite it with something else?
Make a new file, print the first line you want to print, then copy the rest of the other file into the new file. Then delete the old file and rename the new file to the old file.


Third Question:
Quote:Is there a way you can print specifically what is causing an error in a program? If not, it's okay I'll use something else.
You can use ERR, ON ERROR and ERL in your program.

Hope it helped a bit Wink
Quote:Make a new file, print the first line you want to print, then copy the rest of the other file into the new file. Then delete the old file and rename the new file to the old file.

Dodgy
Ok, how did you think it can be done better? (supposed that the line you want to overwrite is not the same length as the original line, because if that is the case I can also think of a better algo... Wink)
Neo's method is fine:
Make a new file, print the first line you want to print, then copy the rest of the other file into the new file. Then delete the old file and rename the new file to the old file.

Here's how I would code Neo's method:
Code:
defint a-z                                     'good habit (not needed here)
dim rc as long                              'record count
open "oldfile" for input as #1
open "newfile" for output as #2
do while not eof(1)
     line input #1,d$                      'D$ means a record data string
     rc=rc+1
     if rc > 1 then print #2,d$        'Put all records except the first
loop
system
If you want, at the end of the program, you can delete "oldfile" and rename "newfile" to "oldfile" from within the program. I wouldn't do it inside the program without some extra code as a guarantee that the job finished completely. I would do it manually from the command line for this example. Otherwise, one little error, and you delete your input file --- bye, bye. Oh, yeah, you were gonna back it up.
*****
Quote:Ok, how did you think it can be done better? (supposed that the line you want to overwrite is not the same length as the original line, because if that is the case I can also think of a better algo... Wink)

I didn't say it could've been done better :wink:
HQS,
The business of the length of the new record being greater that the old record is only an issue if you're trying to overlay the old record. However, nobody has posted this kind of solution, so it's a non-issue.
*****