9

I would like to achieve the following: I ask for a number from the user, run a loop number times, and in each loop run, output the string like this:

-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 ERROR IN 20

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

1
  • 1
    BASIC does not have a string multiplier operation. You need to build it in a loop. Aug 7 at 12:09
9

TL;DR:

Classic approach (will work on most BASICs):

10 INPUT NU        : REM MS-BASIC recognizes only two letters for names
20 A$=""           : REM Initialize a String to collect the hyphens
30 FOR I = 1 TO NU : REM 'I' is traditionally used for counting in BASIC
40 A$=A$+"-"       : REM Add one hyphen per iteration
50 PRINT A$;"TEXT" : REM Spare the expensive sting concatenation, just print them in sequence
60 NEXT I          : REM It's good style to close with variable names

There are of course, many other ways, like using string operations and so on, but they may run in issues (*1), so above is a safe bet.

Details

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

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

In BASIC multiplication is only defined for numbers. It does not operate on strings or mixtures of strings and numbers.

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

BASIC isn't as rich, especially not MS-BASIC. It's ... well ... basic :))

In fact, doing BASIC requires a procedural mentality describing machine operation, not laying out high level concepts. You're telling the interpreter step by step what to do and how to do it. More of a better Assembler than more abstract languages.

So the straight forward (but but kinda clumsy) way would to 'explain' that string 'multiplication' to the interpreter as an inner loop:

Minimalist BASIC

10 INPUT NU
20 FOR I = 1 TO NU : REM loop around all output lines
30 FOR J = 1 TO I  : REM Multiplication-Loop creating the hyphens
40 PRINT "-";      : REM Print one hyphen, let the next PRINT continue right after
50 NEXT J          : REM End of multiplication
60 PRINT "TEXT"    : REM PRINT the text, advance output to the next line
70 NEXT I

This should even work with the tiniest TINY-BASIC. It needs the least amount of RAM, as there is no dynamic string creation and manipulation. Only straight string output from constants. Depending on the BASIC it might as well be faster than seemingly more compact solutions due not doing string operations.

BBB - Better BASIC(s) Bureau

Having said that, there are of course BASIC Dialects that offer functions to build strings by 'multiplying' characters or even strings. BBC BASIC is one of these, offering a STRING$(repeat,string) function that returns a string made of the input string repeated given times. In this case the program will look much like your first approach:

10 INPUT NU
20 FOR I = 1 TO NU
30 PRINT STRING$(I,"-");"TEXT"
40 NEXT I

Simon does it on the C64

To do the same on a C64, you may want to use Simons' BASIC, an extension to V2 BASIC, offering a DUP functions (*2) doing the same:

10 INPUT NU
20 FOR I = 1 TO NU
30 PRINT DUP("-",I);"TEXT"
40 NEXT I

*1 - Like being only valid within certain limits of NU, or eating up lots of RAM.

*2 - Simons BASIC is kind of an outgrown hack, as if not simply named STRING$ like in many other dialects, it should at least have a $ sign attached (DUP$), as it's result is a string.

5
  • 1
    It's been many moons since I did basic so maybe there's trick I'm forgetting, but for your minimal basic, shouldn't line 30 be for j = 1 to i ?
    – Foon
    Aug 5 at 15:11
  • @Foon Drats. You're right. Copy and paste error. Would have never happened in a real BASICC environment were one retypes lines all the time :))
    – Raffzahn
    Aug 5 at 16:45
  • 1
    Lies! I seldom retyped whole lines; I'd do a LIST 50 followed by cursor-up, then typing "55" (and any other changes) and ENTER to duplicate a line. I had a glorious basic which allowed cursor movement. It was awesome. sobs in 6502 hex code @Raffzahn
    – AnoE
    Aug 6 at 13:31
  • @AnoE Do i sense a tiny bit of glorification there? Some of us might remember screen editors constantly screwing up lines when editing, like inserting spaces were not wanted, suddenly changing cursor movements into graphics characters and other nuisances. #RealitCheck101
    – Raffzahn
    Aug 6 at 13:37
  • 2
    It has been about 35 years past. No way I'm misremembering anything... @Raffzahn That said, now that you bring it up, everything you mention still happens these days (inserting space = colleagues with broken space/tab settings in their editors; suddenly changing cursor movements = CLI tools that still don't support cursor movement in 2021; graphics characters = jump servers or shells still not being totally comfortable with utf-8). Some things never change, looks like.
    – AnoE
    Aug 6 at 13:53
8

You can't multiply a string by an integer to repeat it like you can in Python, but if you only ever want to print dashes (or any other single character) and you know the limit of how many dashes you want to print, you could set up a constant string at the start of your program to store some dashes, then use LEFT$() on it as a 'this many dashes' function.

DASHES$ = "------------"
PRINT LEFT$(DASHES$, 3)
PRINT LEFT$(DASHES$, 5)

Output:

---
-----

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.