Qbasicnews.com
April 05, 2020, 12:46:42 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: 1 [2]
  Print  
Author Topic: Addition Loop Problem  (Read 6950 times)
DrV
Na_th_an
*****
Posts: 1553



WWW
« Reply #15 on: January 28, 2006, 12:04:26 AM »

Change str to str$ for QB.
Logged
yetifoot
Ancient Guru
****
Posts: 575



« Reply #16 on: January 28, 2006, 02:06:47 AM »

that line builds a string. sorry it didn't work i couldnt remember how to be QB compatible.

it builds ostr (a string variable that will contain the output ie 1 + 1 = 2)

ostr = str(number) + " + " + str(number) + " = "  + str(number + number)

the '+' that is not within quotes is used to add strings together ie

Code:
s = "Hello "
s = s + "World"

Print s


Quote
Hello World


str is a function that changes a number variable into a string variable, so it can be added in this way.

hope that helps
Logged

EVEN MEN OF STEEL RUST.
RyanKelly
Forum Regular
**
Posts: 109



WWW
« Reply #17 on: January 28, 2006, 03:33:50 AM »

No need for a loop
Code:

dim b as integer

sub doubleit(seed as integer,bound as integer)
   if bound>0 then    
        Print seed; " +" ; seed; " =" ; seed*2
        doubleit seed*2, bound-1
   end if
end sub

Print "Iterations:";
input b
doubleit 1,b


Note that in QB, b>14 will generate an error, and in FB b>30 will cause an oveflow.  In fact, in FB, b=31 will oveflow to -2147483648 which means that b=32 will produce -2147483648 * 2 = 0, which means b=x when x>31 will result in the last x-31 results being 0+0=0
Logged
stylin
Ancient QBer
****
Posts: 445


« Reply #18 on: January 28, 2006, 03:07:26 PM »

Quote from: "RyanKelly"
Note that bad stuff will happen if you try - for some unknown reason - to do this recursively.

Ok, thanks for the advice. Skip the resursive function, Hybrid. :roll:
Logged

stylin:
KiZ
__/--\__
*****
Posts: 2879


WWW
« Reply #19 on: January 29, 2006, 12:30:01 AM »

Im not sure Hybr!d is looking for a recursive function quite yet ;)

He is still trying to learn string concatenation :)
Logged
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #20 on: January 29, 2006, 01:47:19 AM »

It's good that he got a bunch of responses from you all, but...:

Quote
number += number

Quote
? A

Quote
ostr = str(number) + " + " + str(number) + " = "  + str(number + number)

Quote
sub doubleit(seed as integer,bound as integer)


I don't know whether to say "LMAO" or cry.  :cry:
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
SSC
Guru
**
Posts: 288



WWW
« Reply #21 on: January 29, 2006, 02:17:51 AM »

heh i forgot '+=' is an FB thing
Logged

url=http://www.smithcosoft.com]SmithcoSoft Creations[/url]
"If you make it idiot proof, someone will make a better idiot" - Murphy's Law
KiZ
__/--\__
*****
Posts: 2879


WWW
« Reply #22 on: January 29, 2006, 12:07:21 PM »

Quote from: "Agamemnus"
I don't know whether to say "LMAO" or cry.  :cry:


I get the same feeling when I see unindented code. Huh Aga???! huh huh huh!
Logged
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #23 on: January 29, 2006, 08:38:40 PM »

Indented code is just an excuse for complicated code!

And, I don't think += should be allowed at all, nor "?", nor functions for simple things like this, or variables named "ostr", in this example at least...

 :evil:  :evil:  :evil:

Nothing personal.... I just don't like overcomplication OR oversimplification.. somewhat subjective, yes, but still...

I fear I may have created a monster when I suggested touched off the "merging QB/FB" proposition..
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
RyanKelly
Forum Regular
**
Posts: 109



WWW
« Reply #24 on: January 29, 2006, 11:27:17 PM »

Quote from: "stylin"
Quote from: "RyanKelly"
Note that bad stuff will happen if you try - for some unknown reason - to do this recursively.

Ok, thanks for the advice. Skip the resursive function, Hybrid. :roll:


Actually, the same problems will crop up with any of the techniques posted.  Trying to calculate 2^16 in with 16 bit signed integers in QB will generate an overflow error no matter how you do it.
Logged
stylin
Ancient QBer
****
Posts: 445


« Reply #25 on: January 30, 2006, 03:48:13 AM »

RyanKelly, obviously you should use data types that meet - or perhaps exceed - whatever needs you have. If you plan on using numbers bigger than a certain data type can represent, then that's just poor design anyway.

My point was that recursion not only

1. makes this less readily apparent,
2. introduces new ways to confuse a debugger (you and me) about the source of the design problem, and
3. unnecessarily complicates an otherwise easy task.

With recursion, like anything else, use it when it makes code clearer, more elegant, more efficient or whatever your needs happen to be at the time.
Logged

stylin:
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #26 on: January 30, 2006, 05:40:28 AM »

Quote from: "Agamemnus"
[...] I don't think += should be allowed at all, nor "?"


? is in BASIC since the 70s. Try it.
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Agamemnus
x/ \z
*****
Posts: 3491



« Reply #27 on: January 30, 2006, 07:34:16 PM »

Tis, tis an old one in Basic I know... but it's just a bit old..
Logged

Peace cannot be obtained without war. Why? If there is already peace, it is unnecessary for war. If there is no peace, there is already war."

Visit www.neobasic.net to see rubbish in all its finest.
stylin
Ancient QBer
****
Posts: 445


« Reply #28 on: January 30, 2006, 08:11:28 PM »

Quote from: "Agamemnus"
Tis, tis an old one in Basic I know... but it's just a bit old..

So is QBasic ...  :roll:
Logged

stylin:
Pages: 1 [2]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!