New to FreeBASIC? Post your questions here.
Hello1
Posts: 8
Joined: Jan 12, 2009 20:31

Hello everyone! how can I add two numbers in basic not in mathematical way? I mean for example i have a variable (without a dollar sign) with a value of 10, and another one with a value of 15, how can i just add them together to make 1015? Thanks in advance!
McLovin
Posts: 82
Joined: Oct 21, 2008 1:15
Contact:
using -lang fb

sNewString = str(10) + str(15)

Keep in mind that STR will add a leading space if you use -lang qb. In that case, use ltrim to get rid of it.
counting_pine
Posts: 6225
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs
Hi. You can concatenate numbers and/or strings together using the '&' operator. It works the same as the '+' operator does on strings, except it also allows you to concatenate numbers and strings, or numbers and numbers, together.

Code: Select all

`dim a as integer, b as integer, s as stringa = 10: b = 15s = a & bprint s`
This works in all dialects, and you don't have to worry about leading spaces.
Hello1
Posts: 8
Joined: Jan 12, 2009 20:31
hi tried it, but it gives me an error for some reason...
Here is the error compiling with qb:
C:\basic>fbc.exe -lang qb cp\encrypt.bas
cp/encrypt.bas(12) error 67: Array not dimensioned, before '('
EncryptedText = str(5) + str(10)
^

compiling with fb dident work either...
Posts: 360
Joined: Jun 07, 2005 20:59
Location: england, somewhere around the middle
Contact:
try counting_pine's way
Hello1
Posts: 8
Joined: Jan 12, 2009 20:31
hmm that dident work either, i am sure i am doing something really silly... here is the code:
one = 1
two = 2
sum = one & two

here is the error:
C:\basic>fbc.exe -lang qb cp\encrypt.bas
cp/encrypt.bas(14) error 159: Invalid assignment/conversion
sum = one & two
^
Posts: 360
Joined: Jun 07, 2005 20:59
Location: england, somewhere around the middle
Contact:
maybe it should be sum\$ in -lang QB ? (i cant remember)
counting_pine
Posts: 6225
Joined: Jul 05, 2005 17:32
Location: Manchester, Lancs
If sum is not a string, and you know that "two" has 2 digits, then a more efficient way would just be to do:

Code: Select all

`sum = one * 100 + two`

When you concatenate two things with '&' (whether they're numbers or strings) the result will be a string. So it can't be assigned directly to a number variable.

To convert the resulting string back into a number, you can do val(one & two). But as I said above, the "one * 100 + two" method will be more efficient.

EDIT: For what it's worth, McLovin's suggestion will have failed in lang qb because you need to use STR\$ instead. As with other built-in string functions, QB expects a dollar sign on the end.
Last edited by counting_pine on Jan 18, 2009 18:40, edited 1 time in total.
Hello1
Posts: 8
Joined: Jan 12, 2009 20:31
thanks for help! everything worked!
vdecampo
Posts: 2982
Joined: Aug 07, 2007 23:20
Location: Maryland, USA
Contact:
Hello1 wrote:hi tried it, but it gives me an error for some reason...
Here is the error compiling with qb:
C:\basic>fbc.exe -lang qb cp\encrypt.bas
cp/encrypt.bas(12) error 67: Array not dimensioned, before '('
EncryptedText = str(5) + str(10)
^

compiling with fb dident work either...

FYI...In -lang qb Str() must be written Str\$()

-Vince