Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Repeatedly Subtract Numbers
Author Message
CtrlAltDelicious

Posted: 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

Insectoid

Posted: 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

Posted: 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

Posted: 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

Posted: 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?
Insectoid

Posted: 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: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 6 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: