Would need some help again lazarus / 61-Beginners ---------------------------------------------------------------------- guest58006 · 2015-10-05 18:10 I know I may become annoying but since I found this amazing forum, why not use it to find my mistakes easier and fix them? So I've been doing another program which calculates the sum of the numbers inside a number (this may sound confusing, here's an example : 21900 = 2 + 1 + 9 + 2*0 = 12) I've done everything correctly (so glad) but I've encountered a little problem : If I place the "answer line" inside the loop (you'll see the code soon) the line repeats itself to infinity, which is a bit annoying. But if I place it outside the loop, I just introduce the number and then the program stops(not the program itself) it just stays there, doing nothing.. Here's the code, and I would be very grateful if someone could help me repair the mistake and maybe even avoid it in the future : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program modsidiv;var   n,a,b,c,d,e,m: integer;begin writeln('Introduce a number from 1-32000'); readln(n);while n<=32000 dobegina:= n mod 10;b:= (n div 10) mod 10;c:= (n div 100) mod 10;d:= (n div 1000) mod 10;e:= (n div 10000) mod 10;writeln ('The sum is', a+b+c+d+e);end;writeln('Press enter to continue...');readln;end. ---------------------------------------------------------------------- balazsszekely · 2015-10-05 18:18 QuoteI know I may become annoyingYou're not annoying at all! Feel free to ask whatever question you like, you don't have to apologize. QuoteHere's the code, and I would be very grateful if someone could help me repair the mistake and maybe even avoid it in the futureSame mistake as yesterday, you create an infinite loop. In this particular case you don't need that while loop at all. Hope this helps! ---------------------------------------------------------------------- guest58006 · 2015-10-05 18:22 QuoteIn this particular case you don't need that while loop at allYeah, I know.  We've just studied loops and I'm still a bit shaky when using them. I know that I shouldn't use while loop, but we've stopped there, and our homework was to do this. I'm trying to use a loop to not allow people to use numbers higher than 32000. But if I put Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i writeln ('the sum is', a+b+c+d+e); OUTSIDE the repeat loop, Pascal just stops and does nothing. Also when trying to introduce a new value, m which will be inbetween 1-32000 and then just place it inside the while loop instead of 32000, all the answers I'm given are 0.. ---------------------------------------------------------------------- molly · 2015-10-05 18:25 QuoteI'm trying to use a loop to not allow people to use numbers higher than 32000. I'm a bit unsure why one would want to do that. Wouldn't it be more logical to use an if comparison for that ? If n < 32000 then begin   // some code end. However, what could be programmed inside a loop is the divisions you do ;-) ---------------------------------------------------------------------- guest58006 · 2015-10-05 18:33 QuoteHowever, what could be programmed inside a loop is the divisions you do ;-) I've already tried placing this piece of code Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i writeln ('the sum is', a+b+c+d+e);outside the loop, but then Pascal just does nothing.. ---------------------------------------------------------------------- balazsszekely · 2015-10-05 18:50 QuoteI'm trying to use a loop to not allow people to use numbers higher than 32000.This is very strange, but if those are the requirements then: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i //...i := 1;while i <= 32000 dobegin  if i = n then  begin    a:= n mod 10;    b:= (n div 10) mod 10;    c:= (n div 100) mod 10;    d:= (n div 1000) mod 10;    e:= (n div 10000) mod 10;    writeln ('The sum is', a+b+c+d+e);  end;  Inc(i); end;writeln('Press enter to continue...');//... ---------------------------------------------------------------------- Scoops · 2015-10-05 18:54 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program modsidiv;var   n,a,b,c,d,e: integer;begin writeln('Introduce a number from 1-32000'); readln(n);if n<=32000 thenbegina:= n mod 10;b:= (n div 10) mod 10;c:= (n div 100) mod 10;d:= (n div 1000) mod 10;e:= (n div 10000) mod 10;writeln ('The sum is ', a+b+c+d+e);end;writeln('Press enter to continue...');readln;end. ---------------------------------------------------------------------- Handoko · 2015-10-05 19:04 My guess: ... ... readln(n); while (n <= 32000) do begin    ... // do the calculation    ... // show the result    readln(n); end; ... ... Note: the code above is not actual code. ---------------------------------------------------------------------- molly · 2015-10-05 19:09 Since OP stated to be learning about loops, i would guess that his/her problem is about how to apply loops. I'm not giving an exact answer here, but please analyze the following code and notice the difference. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program loop1; // counting numbers 'manually'Procedure count1;var   x: integer;  a,b,c,d,e : integer;begin  x := 1000;  a := x + 1;  b := x + 2;  c := x + 3;  d := x + 4;      e := x + 5;   writeln('the sum = ', a + b + c + d + e);end;  // counting numbers in a somewhat 'smarter' wayProcedure count2;const   min = 1;  max = 5;var   i: integer;  x: integer;  sum : integer;begin  i   := min;  sum := 0;  x   := 1000;   while i <= max do  begin    writeln('x + ', i, ' = ', x + i);    sum := sum + x + i;    i := i + 1;  end;  writeln('the sum = ', sum);end;  begin  Count1;  Count2;end.  not implying Handoko's suggestion is wrong, as it might indeed be the case. ---------------------------------------------------------------------- Handoko · 2015-10-05 19:28 Advice for aerowei and beginners: Indent your code properly, looping will become much easier. aerowei's code didn't indent properly. It doesn't seem too bad (for now). But you will see big different if your code reaches hundreds of lines. Learn to code neatly if you want to become a great programmer. What is neat code? See the code of GetMem and molly. ---------------------------------------------------------------------- rvk · 2015-10-05 19:43 If you really want/need to use a loop, I would use it for incrementing the div parameter so you would only need one add-statement within the loop. Something like this: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program modsidiv;var  n, i, sum: integer;begin  writeln('Introduce a number from 1-32000');  readln(n);  sum := 0;  i := 1;  while i <= 32000 do  begin    sum := sum + (n div i) mod 10;    i := i * 10;  end;  writeln('The sum is ', sum);  writeln('Press enter to continue...');  readln;end.Once i becomes 100.000 (is >32.000) the loop will end. A whole other approach would be to read the number as a string and add the numbers that are in the string. That way you're not limited to 32.000 (or any larger integer-type). But my guess is that that's not according to the assignment. Edit: Woops. That's probably what molly was trying to show and I gave away too much here. Sorry. ---------------------------------------------------------------------- Mando · 2015-10-05 19:47 Hi, if you want to use a loop in sum calculating, you can use this code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  sum:=0;  while n>0 do  begin    sum:=sum+n mod 10;    n:=n div 10;  end; In addition, the number (n) can have any number of digits. Regards ---------------------------------------------------------------------- molly · 2015-10-05 19:48 Quote from: aerowei on October 05, 2015, 08:33:19 pmQuoteHowever, what could be programmed inside a loop is the divisions you do ;-) I've already tried placing this piece of code Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i writeln ('the sum is', a+b+c+d+e);outside the loop, but then Pascal just does nothing.. Well, since the writeln is then placed _outside_ the while loop, the used variables in that writeln doesn't get calculated, now would they ? So how do you expect the writeln do produce sensible output ? See also rvk's answer. edit: @rvk: yeah, i guessed what the problem might be and you indeed gave a little too much hint imho ;-p No problem, a simple miscommunication (and you don't seem to have been the only one). @aerowei: a small hint. Don't just copy-paste the proposed code, probably checking if the codes works for you. Try to understand what happens and why. The key is here to learn how to interpret your assignment and finding a proper solution to solve the problem presented in the assignment. Simply copying the code doesn't make you learn anything, unless you understand the why. ---------------------------------------------------------------------- guest58006 · 2015-10-05 19:48 THANK YOU SO MUCH FOR ALL YOUR AMAZING RESPONSES! To be honest, I didn't expect so many answers from you guys. Thank you so much for taking your time to help me solve this (seemingly little) problem. In the end I chose Handoko's code, even though I'm sure Scoop's and Getmem's would work aswell (tested them both), I must use only what we've been given until now, and sadly I can't use "if" or "inc" in my code (yet?). I might just end up asking my teacher if I can use some lines of code I've learned on the internet, etc. Since our subject were the loops, I had to use Handoko's code, since it was the simpliest one. QuoteAdvice for aerowei and beginners: Indent your code properly, looping will become much easier. aerowei's code didn't indent properly. It doesn't seem too bad (for now). But you will see big different if your code reaches hundreds of lines. Learn to code neatly if you want to become a great programmer. What is neat code? See the code of GetMem and molly Yeah, you are right here, I'm just starting with programming (we don't even have that many lessons per week, only 1, wtf, but my biggest worry isn't indenting at the moment, but as you told me, in the future I WILL take care of it) I've been looking at molly's code for about 5-10 mins and I seem to understand some most of it (maybe it's just me being tired and I hope tommorow I'll think clearer), but maybe because my logic isn't the shiniest of all and I can't understand some things which may sound hard for me. Mind explaining what you did in the 2nd count? EDIT: Oh wow... 3 more replies , in such a short time - you're amazing guys! I'll take a look tommorow, but I still have to stay with Handoko, as we're just starting Pascal, and I'm sure my teacher won't even like the idea of me copying someone's code instead of doing it myself, and also I think (after take a very quick look) some of you have gone further and tried explaining me how to use sums in a loop.. I really appreciate that, but I do not know if I need that yet. ---------------------------------------------------------------------- molly · 2015-10-05 20:55 Quote from: aerowei on October 05, 2015, 09:48:34 pmI've been looking at molly's code for about 5-10 mins and I seem to understand some most of it (maybe it's just me being tired and I hope tommorow I'll think clearer), but maybe because my logic isn't the shiniest of all and I can't understand some things which may sound hard for me. Mind explaining what you did in the 2nd count? Hmz. i could, but then you would not learn anything (in fact i've written down my whole analyses and wanted to post until i realized that's your job ;-) ). If you really don't fully understand how routine1 evolved into routine2, then please ask me anything specific what i can explain to you, but please don't expect me to analyze routine1 which should be your first goal. If you analyze routine1 properly (please use a clear head for that) then information can be extracted that leads to the solution used in routine2. Asking questions and recognizing the pattern in routine1 automatically leads to something as shown in routine2. As a hint, when not fully understanding what happens, then you can always create small example routines that mimics things. for example change the while loop inside routine2 into something that reads: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  while i <= max do  begin    writeln('The value of i = ', i);    i := i + 1;  end; And see for yourself what this outputs. Using such small examples make you understand what the code actually does. In this case you will see that the loop increases variable i until it has reached a particular value. The variable i also started at a particular value. The loop itself print out all the values while being inside the loop. Hopefully that will let you understand how a while loop works. The fact that i do some calculation inside the loop is secondary (even while in the end it is used to produce the result), but could just as easily be analyzed by yourself ;-) FWIW: in case you have been given a clear assignment, then don't hesitate to literally post the assignment in the forum so that we know for sure how to advise you. But please don't expect users to give you a full example that meets the requirements of your assignment. In case someone would post a fully working example that meets the requirements then please ignore that, as it won't help you learn anything. Programming can be hard to learn, but becomes even harder when just copy-pasting someone else's code without fully understanding the code. Analyzing presented working code in hindsight is always more easy then doing things yourself. Doing things yourself has the benefit of sticking into memory better (at least that goes for most people, i am aware there are exceptions). So, in the end i wanted to ask you if you could explain what happens exactly in routine2, because if you can tell me that then you figured out how to solve the repeating div problem from your original question ;-) (well, at least you should be able todo so then). ---------------------------------------------------------------------- shobits1 · 2015-10-05 23:14 Quote from: aerowei on October 05, 2015, 09:48:34 pmI must use only what we've been given until now, and sadly I can't use "if" or "inc" in my code (yet?) ???  so they teach you loops before decision making (comparison) ;; are you sure you didn't skip classes  any way rvk, gave the most valid code -in my opinion-; so you should at least study it, and comprehend how it works ---------------------------------------------------------------------- guest58006 · 2015-10-06 17:34 QuoteMy guess: ... ... readln(n); while (n <= 32000) do begin    ... // do the calculation    ... // show the result    readln(n); end; ... ... Note: the code above is not actual code. I know this might seem strange.. but what does Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i readln(n); do that stops the loop?  I've asked my best friend (who also goes to programming with me) and he didn't know aswell. Care explaining? Quote???  so they teach you loops before decision making (comparison) ;; are you sure you didn't skip classes  any way rvk, gave the most valid code -in my opinion-; so you should at least study it, and comprehend how it works We didn't use inc, but I know what it does. We learned if, and we've had enough assignments with "if" "case of" that we've done already.. etc etc, cause I mean I would've come to this forum even earlier if I didn't know how to use them. QuoteSo, in the end i wanted to ask you if you could explain what happens exactly in routine2, because if you can tell me that then you figured out how to solve the repeating div problem from your original question ;-) (well, at least you should be able todo so then). Sure, no problem ! But, sadly neither today nor the next days.. I've got some other things to do (school and extra work for other objects), but I will surely message you with my answer/questions regarding your code on Friday/Thursday! Thanks again for your amazing response! FOR EVERYBODY READING MY REPLIES: I'VE JUST STARTED PROGRAMMING - which means I'm still new to it. I've skipped two lessons(classes, anything you may call it) for personal reasons, but I've recovered everything they've studied. I know this might seem strange, but I'm completely new to programming, I've only been to about 3-4 lessons/classes in TOTAL, which means I still have yet to learn and there are a lot of holes in my understanding. I still have lots of questions left unanswered and thus I came here. I know, I have to learn everything by myself.. but sometimes my logic doesn't shine...at all.. it may be just me who blocks myself from trying to understand something thinking it's too hard and I do not need it. Sometimes i'm a fast learner, sometimes I can't understand something that seems so simple, just because I'm giving up fast and just making myself think I'm not going to understand this. I'm not trying to say that all your answers are worthless, they are wonderful and I really appreciate all your hard work you've put into answering (possibly) a simple question (for you) FOR my understanding. But I'm still a beginner in programming and sometimes all those pieces of code just confuse me. Still, a big thanks to everyone who contributed to the answers. I WILL take a look at all of them (and possibly even more) on Thursday/Friday and leave a reply then. Thanks again, and have a nice day! ---------------------------------------------------------------------- Handoko · 2015-10-06 18:01 Quote from: aerowei on October 06, 2015, 07:34:11 pmI know this might seem strange.. but what does Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i readln(n); do that stops the loop? The thing that stops the loop is if the condition is not fulfilled, in your case the condition is this one: (n <= 32000) The basic while loop construct is: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i while (condition_fulfilled) do begin  ... // do somethingend; Now we change the condition with the real thing: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i while (n <= 32000) do begin  ... // do somethingend; But you can't simply use it before you set the value for the condition testing. In your case, the condition testing is to test the value of n. So we added a new line: ... set the value for n Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ... // set the value for nwhile (n <= 32000) do begin  ... // do somethingend; Because the input for n is user key pressing, so we change the line with readln(n): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i readln(n);while (n <= 32000) do begin  ... // do somethingend; If you pay attention on the code above, it means if user provide a value less than or equal to 32000 then the condition is fulfilled. In other words, it means: to stop the looping, user need to provide a value bigger than 32000. For example 33000. But the while looping is not finished yet. You need to set the value for the condition testing twice. The second one usually just before the end of the loop. So the code become: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i readln(n);while (n <= 32000) do begin  ... // do something  readln(n);end; Hope you can understand my explanation. QuoteI'VE JUST STARTED PROGRAMMING - which means I'm still new to it. I've skipped two lessons(classes, anything you may call it) for personal reasons, but I've recovered everything they've studied. I know this might seem strange, but I'm completely new to programming, I've only been to about 3-4 lessons/classes in TOTAL, which means I still have yet to learn and there are a lot of holes in my understanding. I still have lots of questions left unanswered and thus I came here. If you have problems or questions with programming, here you can find lots of people are nice and willing to help. QuoteI know, I have to learn everything by myself.. but sometimes my logic doesn't shine...at all.. it may be just me who blocks myself from trying to understand something thinking it's too hard and I do not need it. Sometimes i'm a fast learner, sometimes I can't understand something that seems so simple, just because I'm giving up fast and just making myself think I'm not going to understand this. You just need more practices and time. Don't push yourself to understand the things that too far from your ability to understand. Programming is a fun thing, so have fun with programming. Try to program something that is fun, like how to make your computer to play sounds, how to draw something on the screen, how to make object moving. After you have more knowledge of controlling those things, then try to make a computer piano, animation or games. ---------------------------------------------------------------------- shobits1 · 2015-10-06 21:31 aerowei, so you have learned 'if .... then' after all. inc is not big deal inc(x) is the equivalent to x:=x+1 well, in programming you should always use the right tool to solve the problem, and unless the assignment explicitly say you should use loop to restrain the user to certain n number; then you shouldn't use the loop but use 'if .... then'. remember that certain assignment will require you to use skills you learned in past lessons without a hint; and in this case (the assignment) I see it requires the use of 'if...' and 'while...'; and should be use like this. 1.write message and read the  n (number) entered by the user. 2.check if the n has met the requirement, using if...then statement. 2.a.    if n has not met the requirement display error message then exit the program. 2.2.    if n has met the requirement go to 3 3.enter loop while n > 0 3.a     inside the loop use m = n mod 10 to get the the 1st digit of n and add it to previous m. 3.b     inside the loop use n div 10 to discard the 1st digit in n and save it back to n. 3.c     eventually the n will be equal 0 at certain point and the loop will exit. 4.display m the sum of the digits of n 5.exit the program. this will result in clear and clean program. ---------------------------------------------------------------------- molly · 2015-10-06 23:46 Quote from: aerowei on October 06, 2015, 07:34:11 pm I know this might seem strange.. but what does Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i readln(n); do that stops the loop? I've asked my best friend (who also goes to programming with me) and he didn't know aswell. Care explaining? Very simple. The behaviour of readln is described here. QuoteRead reads one or more values from a file F, and stores the result in V1, V2, etc. After that it goes to the next line in the file. The end of the line is marked by any of the supported line ending styles, independent of the platform on which the code is running (supported line ending styles are CRLF, LF or CR). The end-of- line marker is not considered part of the line and is ignored. But since you're not reading from a file, rather from input the following applies: QuoteIf no file F is specified, then standard input is read. The variables V1, V2 etc. must be of type Char, Integer, Real, String or PChar. But, the upper remark still counts: input is being read until an end of line marker is encountered. Or to put it into other words: the ReadLn command waits just as long as the user pressed the enter key, then it will store the by the user entered value from input into the variable for you to work with. Out of curiosity and for other newbies out there: When programming a solution for these assignments, don't you use Lazarus and/or FPC ? if so, then are you aware that help can be used? If you use Lazarus and you have your code in the code-editor window, then the editor allows you to position the cursor over any word. As soon as you press the F1 key, the editor will pop up the specific help page for that 'active' word (as long as that word is a Pascal related/recognized word). FP-IDE has something similar, but i believe the help must be installed manually in that case. As an alternative you can download f.e. the windows CHM files, which you could invoke manually, in the case you are using a complete other editor. Such files can manually be searched for keywords (like readLn), and the same help-page will be displayed for you. Although such help pages don't explain everything there is to learn on how to program in Pascal, it does offer you to get familiar with the keywords, RTL, and additional units/routines that are part of the Free Pascal programming language. ---------------------------------------------------------------------- Handoko · 2015-10-07 03:00 To aerowei. If you can understand my previous explanation about while looping, then you should try what shobits1 said (rvk' code).  It is more complex because it uses looping for process the mod calculation. But it's worth to study. molly mentioned CHM-file. Yes it is very useful, I used it frequently. If you're interested, here is the link: http://wiki.freepascal.org/Installing_Help_in_the_IDE And these are some useful resources that worth to bookmark: http://www.freepascal.org/docs.var http://www.freepascal.org/docs-html/ref/ref.html#refch3.html http://www.freepascal.org/docs-html/rtl/system/index-5.html http://wiki.freepascal.org/LCL_Components Also these are good places to start (for beginners only): http://www.pascal-programming.info/index.php http://www.taoyue.com/tutorials/pascal/index.html