
-----------------------------------
baykko
Mon Sep 15, 2008 7:18 pm

finding sum of all numbers until an eof of -99 is entered?
-----------------------------------
i have kind of forgotten my basic turing knowledge
i am required to create a program where you can enter an unlimited amount of numbers, and it will end once you enter -99 and it will find the sum of all the numbers that you entered
i only know how to do this for a set number of entries, but how would I be able to keep entering an unlimited amount of numbers with no limit?

-----------------------------------
Insectoid
Mon Sep 15, 2008 7:53 pm

RE:finding sum of all numbers until an eof of -99 is entered?
-----------------------------------
You would have a loop, with an exit when num= -99, then get a number from the user and add it to a variable 'total'.

-----------------------------------
baykko
Mon Sep 15, 2008 8:04 pm

Re: finding sum of all numbers until an eof of -99 is entered?
-----------------------------------
how can i make it so when i enter the eof of -99, it isnt added to my total sum? this is what ive done so far

    var numb, totalsum : int := 0
    loop
        get numb
        totalsum := totalsum + numb
        exit when numb = -99
    end loop
    put totalsum


-----------------------------------
Insectoid
Mon Sep 15, 2008 8:09 pm

RE:finding sum of all numbers until an eof of -99 is entered?
-----------------------------------
you do the exit when at the top of the loop, the first thing

-----------------------------------
baykko
Mon Sep 15, 2008 8:14 pm

Re: finding sum of all numbers until an eof of -99 is entered?
-----------------------------------
I moved the exit in between get numb and totalsum:= and it works properly now
thanks guys

-----------------------------------
Insectoid
Mon Sep 15, 2008 8:16 pm

RE:finding sum of all numbers until an eof of -99 is entered?
-----------------------------------
no, no, no, put it at the VERY TOP of your loop. This is important as you get into larger programs.

-----------------------------------
baykko
Mon Sep 15, 2008 8:18 pm

Re: finding sum of all numbers until an eof of -99 is entered?
-----------------------------------
when i put it at the very top of the loop, the -99 gets added to my totalsum

-----------------------------------
gitoxa
Mon Sep 15, 2008 8:19 pm

Re: RE:finding sum of all numbers until an eof of -99 is entered?
-----------------------------------
no, no, no, put it at the VERY TOP of your loop. This is important as you get into larger programs.

He's got it in the right place.  There isn't a set place for an exit to go, it's just wherever it'll be most efficient.  In this case, it's where he's moved it to.
