# Qbasicnews.com

## QBasic => QB Discussion & Programming Help => Topic started by: Hybr!d on January 27, 2006, 08:00:17 AM

 Title: Addition Loop Problem Post by: Hybr!d on January 27, 2006, 08:00:17 AM Hello guys. I have another problem this time.I am trying to make a program that keeps on adding the number that is found.Example:1) User put input of how many time loop occurs.2) 1 is first number then is added by itself. Then keeps going on like this1 + 1 = 22 + 2 = 44 + 4 = 83) Then stops at the number they specified. Title: Addition Loop Problem Post by: SSC on January 27, 2006, 08:08:37 AM Code:screen 12dim loop_max as integerdim number as integerdim result as integerresult = 0print "Enter number of loops"input loop_maxprint "Enter number to be multiplied"input numberfor count = 1 to loop_max  result += numbernextprint resultsleepif you have any question reguarding this just ask =) Title: Addition Loop Problem Post by: na_th_an on January 27, 2006, 08:20:06 AM That's not quite what he asked for.Code:n% = 1Input "Iterations"; iterations%For i% = 1 To iterations%   n% = n% + n%Next i%Print n%Anyway, no need for loops:Code:Input "Iterations"; iterations%Print 2^iterations%Anyway, I'm not for posting code if the requester doesn't demonstrate he has, at least, tried. I did this time 'cause SSC's solution was for a different problem :) Title: Addition Loop Problem Post by: Hybr!d on January 27, 2006, 09:08:11 AM nathan none of them are working. The second one works but only squares it by 2. Im looking for like it to go up by 1 it self so when it finds a answer it does that one then when it gets that answer another. Title: Addition Loop Problem Post by: na_th_an on January 27, 2006, 09:56:54 AM My progs do what you specify in your first post. Explain it correctly, and we'll be able to help you.Quote from: "Hybr!d"1) User put input of how many time loop occurs.2) 1 is first number then is added by itself. Then keeps going on like this1 + 1 = 22 + 2 = 44 + 4 = 83) Then stops at the number they specified.1, 2 and 3 just describe an algorithm to find a power of two.Have you tried to solve this [strike]homework[/strike] problem? Show us what you got so far and we'll be able to help you fill in the gaps :)And I don't understand this text:QuoteIm looking for like it to go up by 1 it self so when it finds a answer it does that one then when it gets that answer another. Title: Addition Loop Problem Post by: Hybr!d on January 27, 2006, 10:09:30 AM nathan I mean like htis more clearly.Say this is the program******************************************************Welcome to ProgramHow many Loops or whatever:  41+1=22+2=44+4=88+8=16*****************************************************Thats what I mean.. Gets the answer then it adds it self then does it again. Till the end of specification. Title: Addition Loop Problem Post by: SSC on January 27, 2006, 10:33:20 AM ya that what i made my code example to do, but i realized that you need to replaceCode:result += numberwithCode:number += number so that its always adding to its self (including the first time) Title: Addition Loop Problem Post by: Hybr!d on January 27, 2006, 10:56:56 AM SSC your code doesnt seem to work!! Ive got no idea what to do Title: Addition Loop Problem Post by: KiZ on January 27, 2006, 11:34:03 AM Code:input loopsnumber = 1for i = 1 to loops   number = number + number   print numbernextIf I understand exactly what he wrote... He wanted it to print the result every iteration. Title: Addition Loop Problem Post by: na_th_an on January 27, 2006, 12:04:17 PM Our programs do exactly what the guy is asking for, only that they just give the final answer instead of showing every step.If you want the program to output every step,  you just have to add a PRINT to the loop, as KiZ mentioned. The changes are very simple and I think you should try to do them. We are not here to do your homework, but to help you doing it :) Title: Addition Loop Problem Post by: axipher on January 27, 2006, 12:49:34 PM Do exactly what KiZ saidCode:input loopsnumber = 1for i = 1 to loops   number = number + number   print numbernextAnd if it closes right away add Sleep to the end. Title: Addition Loop Problem Post by: Dio on January 27, 2006, 03:47:43 PM i think he means this:Code:A = 1do A = A + A? Aloop until [insert END number here ex: loop until a = 1048576] Title: Addition Loop Problem Post by: Hybr!d on January 27, 2006, 08:37:01 PM Hey Kiz thanx it works a charm.. I tried to put a save function to save all numbers to a text file... I did this but it only saves the last number generated.Code:CLSINPUT loopsnumber = 1FOR i = 1 TO loops   number = number + number   PRINT numberNEXTPRINTINPUT "Save as: ", sf\$OPEN sf\$ FOR OUTPUT AS #1WRITE #1, numberCLOSE #1Also how would I show the 1 + 1 = 2 instead of showing 2? Title: Addition Loop Problem Post by: yetifoot on January 27, 2006, 10:52:32 PM Code:Dim ostr As StringCLSINPUT "Loopa?  : ", loopsnumber = 1INPUT "Save as : ", sf\$OPEN sf\$ FOR OUTPUT AS #1FOR i = 1 TO loops   ostr = str(number) + " + " + str(number) + " = "  + str(number + number)   Print #1, ostr   Print ostr   number = number + numberNEXTCLOSE #1 Title: Addition Loop Problem Post by: Hybr!d on January 27, 2006, 11:44:50 PM yetifoot I dont understand this line in your codeCode:ostr = str(number) + " + " + str(number) + " = "  + str(number + number)and in qbasic it says it  is a type mismatch in " + ".Could anyone please explain? Title: Addition Loop Problem Post by: DrV on January 28, 2006, 12:04:26 AM Change str to str\$ for QB. Title: Addition Loop Problem Post by: yetifoot 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 ieCode:s = "Hello "s = s + "World"Print sQuoteHello Worldstr is a function that changes a number variable into a string variable, so it can be added in this way.hope that helps Title: Addition Loop Problem Post by: RyanKelly on January 28, 2006, 03:33:50 AM No need for a loopCode:dim b as integersub doubleit(seed as integer,bound as integer)   if bound>0 then             Print seed; " +" ; seed; " =" ; seed*2        doubleit seed*2, bound-1   end ifend subPrint "Iterations:";input bdoubleit 1,bNote 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 Title: Addition Loop Problem Post by: stylin 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: Title: Addition Loop Problem Post by: KiZ 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 :) Title: Addition Loop Problem Post by: Agamemnus on January 29, 2006, 01:47:19 AM It's good that he got a bunch of responses from you all, but...:Quotenumber += numberQuote? AQuoteostr = str(number) + " + " + str(number) + " = "  + str(number + number) Quotesub doubleit(seed as integer,bound as integer)I don't know whether to say "LMAO" or cry.  :cry: Title: Addition Loop Problem Post by: SSC on January 29, 2006, 02:17:51 AM heh i forgot '+=' is an FB thing Title: Addition Loop Problem Post by: KiZ 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! Title: Addition Loop Problem Post by: Agamemnus 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.. Title: Addition Loop Problem Post by: RyanKelly 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. Title: Addition Loop Problem Post by: stylin 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 only1. makes this less readily apparent,2. introduces new ways to confuse a debugger (you and me) about the source of the design problem, and3. 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. Title: Addition Loop Problem Post by: na_th_an 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. Title: Addition Loop Problem Post by: Agamemnus on January 30, 2006, 07:34:16 PM Tis, tis an old one in Basic I know... but it's just a bit old.. Title: Addition Loop Problem Post by: stylin 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: