Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Repeatedly Subtract Numbers
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
CtrlAltDelicious




PostPosted: Thu Jun 15, 2017 9:59 am   Post subject: Repeatedly Subtract Numbers

What is it you are trying to achieve?

I'm trying to find a way so that the user enters how many numbers they want to subtract, and then they can type the numbers (pressing Enter for each number). Finally, the result of subtracting all those numbers will be displayed. (Example: 6-3-3=0)


What is the problem you are having?

I have it in a loop, as it probably should be, but I can't seem to figure out a way to calculate any amount of numbers above 2...


Describe what you have tried to solve this problem

Again, I put them in a regular loop, with a counter to keep track & it is set to exit when it reaches the amount of numbers the user has said previously.


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

put "How many numbers would you like to subtract? " ..
get exitwhen
put ""
if exitwhen <= 1 then
put "Error!"
exit
end if
put "Please enter the series of numbers you would like to subtract (press Enter after typing each number):"
put ""
loop
counter := counter + 1
exit when counter = exitwhen
if counter = 1 then
get number1
end if
if counter >= 2 then
total := total - number2
end if
if counter = exitwhen then
exit
end if
get number2
total := number1 - number2

end loop
put ""
put "The result of subtracting all ", exitwhen, " of your numbers is ", total, "!"

Please specify what version of Turing you are using

Turing 4.1.1
Sponsor
Sponsor
Sponsor
sponsor
Insectoid




PostPosted: Thu Jun 15, 2017 10:32 am   Post subject: RE:Repeatedly Subtract Numbers

You're trying to use number2 inside the loop, but you don't actually get number2 until after the loop has executed. You can't use a variable before you've given it a value.

If you want to get number2 more than one time...maybe you should get it inside the loop?
CtrlAltDelicious




PostPosted: Fri Jun 16, 2017 7:34 am   Post subject: Re: Repeatedly Subtract Numbers

Wait, maybe if I could find a way to keep the previous number that the user entered..?
Insectoid




PostPosted: Fri Jun 16, 2017 3:05 pm   Post subject: RE:Repeatedly Subtract Numbers

Why do you need to keep the previous number? Once you've subtracted it, what else do you need it for?
CtrlAltDelicious




PostPosted: Mon Jun 19, 2017 9:06 am   Post subject: Re: Repeatedly Subtract Numbers

Since I need to repeatedly subtract numbers entered by the user, I think it would be useful to keep updating the previous integer - how might I be able to do that, or possibly a different method that would do the same? Question
Insectoid




PostPosted: Mon Jun 19, 2017 11:58 am   Post subject: RE:Repeatedly Subtract Numbers

I think you might do well with one variable to hold the running total, and another to handle all input.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 6 Posts ]
Jump to:   


Style:  
Search: