Computer Science Canada

HELP loop problem

Author:  dadoor [ Sat Oct 30, 2010 10:57 am ]
Post subject:  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.
<Replace all the <> with your answers/code and remove the <>>


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
<Answer Here>


Describe what you have tried to solve this problem
i tried sum= payroll +sum but it didnt work
<Answer Here>


Post any relevant code (You may choose to attach the file instead of posting the code if it is too long)
<Answer Here>

Turing:


<Add your code here>



Please specify what version of Turing you are using
<Answer Here>

Author:  TerranceN [ Sat Oct 30, 2010 12:20 pm ]
Post subject:  RE:HELP loop problem

You need to declare your sum variable outside of your loop, for example:

Turing:
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.

Author:  dadoor [ Sat Oct 30, 2010 1:59 pm ]
Post subject:  Re: HELP loop problem

sorry im very new to this, i dont understand the use of the strintok and strint

Author:  dadoor [ Sat Oct 30, 2010 2:07 pm ]
Post subject:  Re: HELP loop problem

i used real for the variable instead of string, am i suppost to use string?

Author:  dadoor [ Sat Oct 30, 2010 2:49 pm ]
Post subject:  Re: HELP loop problem

ive got it to work but can i use real for the whole thing becuase i want decimals 0.o

Author:  TerranceN [ Sat Oct 30, 2010 3:10 pm ]
Post subject:  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.

Author:  dadoor [ Sat Oct 30, 2010 9:59 pm ]
Post subject:  Re: HELP loop problem

k thanks it works now Smile


: