
-----------------------------------
dadoor
Sat Oct 30, 2010 10:57 am

HELP loop problem
-----------------------------------
What is it you are trying to achieve?
i am writing a program that calculates the payroll for the user, i used loop to loop the program but i want to be able to
add the sums of each time it is used.



What is the problem you are having?
i dont know how to make it so that it will add up the total number of sums everytime it is used



Describe what you have tried to solve this problem
i tried sum= payroll +sum but it didnt work



Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)








Please specify what version of Turing you are using


-----------------------------------
TerranceN
Sat Oct 30, 2010 12:20 pm

RE:HELP loop problem
-----------------------------------
You need to declare your sum variable outside of your loop, for example:

var sum : int := 0

loop
    var input : string
    get input
    if (~strintok(input)) then
        put "invalid integer, input not counted"
    else
        sum += strint(input)
    end if
    
    put "Sum is now: " + intstr(sum)
end loop


Also, variable += number is the same as saying variable = variable + number.

-----------------------------------
dadoor
Sat Oct 30, 2010 1:59 pm

Re: HELP loop problem
-----------------------------------
sorry im very new to this, i dont understand the use of the strintok and strint

-----------------------------------
dadoor
Sat Oct 30, 2010 2:07 pm

Re: HELP loop problem
-----------------------------------
i used real for the variable instead of string, am i suppost to use string?

-----------------------------------
dadoor
Sat Oct 30, 2010 2:49 pm

Re: HELP loop problem
-----------------------------------
ive got it to work but can i use real for the whole thing becuase i want decimals 0.o

-----------------------------------
TerranceN
Sat Oct 30, 2010 3:10 pm

RE:HELP loop problem
-----------------------------------
Those two functions are just to make sure the input is actually a number. You could just use a real type variable for the input, but you run the risk of a user entering something incorrect and your program crashing.

strint takes a string and gets the integer representation of it (theres also strreal to get a real number representation of it). And since the user may accidentally enter something thats not a number, you can use strintok (or strrealok for real numbers) to make sure it can be properly converted. Finally the ~ symbol means 'not' in turing, so my if statement would read something like, if the string cannot be converted properly, then print an error message, else add the converted string to the sum variable.

Also, on this forum if no one has posted after you post, you can just edit your last post instead of making another post.

-----------------------------------
dadoor
Sat Oct 30, 2010 9:59 pm

Re: HELP loop problem
-----------------------------------
k thanks it works now :)
