3

I would like to reach the following: I ask for a number from the user, and then output a string like the following:

-STR$

--STR$

---STR$

----STR$

-----STR$

I tried to do this:

10 INPUT NUM%
20 FOR X=1 TO NUM%: PRINT NUM%*"-" + "TEXT" : NEXT

The code above got me an error: ?TYPE MISMATCH EROR IN 20

However, I didn't yet figure out how to manipulate the string's beginning to multiply the '-' marks on each loop run

K. Gero
  • 67
  • 5
  • 1
    I edited my post by addig the code I tried to use. As it turned out it would be a too high-level solution because I got an error that says the following: "?TYPE MISMATCH EROR IN 20" – K. Gero Aug 05 '21 at 11:29

3 Answers3

2

Maybe this:

10 INPUT NUM%
20 FOR I = 1 TO NUM%
30 FOR J = 1 TO I: PRINT "-"; : NEXT
40 PRINT " TEXT"
50 NEXT

There is no multipy of strings/character, as far as I remember to old (good) times.

MiniMik
  • 268
  • 1
  • 10
1

I believe even older, more primitive forms of BASIC had the STRING$() function. It takes two parameters: the number of times to repeat the character and the character itself. So...

10 INPUT NUM%
20 FOR X=1 TO NUM%: PRINT STRING$(NUM%, "-") + "TEXT" : NEXT
Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
  • I'm still getting the type mismatch error in 20 – K. Gero Aug 05 '21 at 11:59
  • It's been a while since I've coded in BASIC even though it was my primary language (I'm retired) but it's probably one of two things: Try replacing the plus sign (+) with a semi-colon (;). If that doesn't work, it might be that I have the parameters backward for STRING$() so try STRING$("-", NUM%). If STRING$ was not valid you'd be getting a SYNTAX ERROR instead, so it must be a valid function. – Bill Hileman Aug 05 '21 at 13:03
  • 2
    I don't think that the commodore BASIC (like many of the 6502 BASICs at the time) had the `STRING$()` function. It did have a `LEFT$()` though. An easy substitution: ` PRINT LEFT$("----------",NUM%); "TEXT"` – Sep Roland Aug 05 '21 at 21:50
1

An alternative:

100 INPUT NM%
110 BR$="----------"
120 PRINT LEFT$(BR$,NM%);
130 PRINT "TEXT"

This eliminates the need for an expensive FOR loop, and should be okay as long as NM% is not greater than the length of BR$.

One other thing to point out is that your variable names are effectively capped at two characters, e.g.:

The length of variable names are optional, but max. 80 chars (logical input line of BASIC). The BASIC interpreter used only the first 2 chars for controlling the using variables. The variables A$ and AA$ are different, but not AB$ and ABC$.

(Source: https://www.c64-wiki.com/wiki/Variable). For that reason I used NM% instead of NUM%; it will prevent issues later.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
tendim
  • 447
  • 3
  • 10
  • If we care about efficiency, then we should not write 2 separate `PRINT` statements, but write `120 PRINT LEFT$(BR$,NM%);"TEXT"`. There's considerable overhead for every line in the program. – Sep Roland Sep 12 '21 at 01:31
  • Absolutely true. But because this is BASIC my view is that you would be better off writing for clarity, and then using a tool to compress it (or compile it). "Make it work, then make it fast." – tendim Oct 01 '21 at 15:23