Basic R Question - updatable input in while loop
up vote
0
down vote
favorite
still in the learning stages of R. Trying to setup a basic piece of code that will allow me to have a user enter numbers until they enter "0", at which time the program will sum entries and display to the user. This is what I have so far:
print ("Enter a number.enter 0 when finished")
enterednum <-as.integer(readLines(con=stdin(),1))
finalnum = 0
while (enterednum != 0)
print ("Enter another number. enter 0 when finished");
newnum <-as.integer(readLines(con=stdin(),1));
finalnum <- (enterednum + newnum)
print(paste("The sum of your numbers is", finalnum,"."))
The point of the exercise is to use while statements. While the false condition of the While statement (entering "0") works, any time the initial input is anything but 0, I receive a debug error for any lines after the while statement. Been wracking my brain and digging through here, but not been able to figure it out. Any help is appreciated.
r
add a comment |
up vote
0
down vote
favorite
still in the learning stages of R. Trying to setup a basic piece of code that will allow me to have a user enter numbers until they enter "0", at which time the program will sum entries and display to the user. This is what I have so far:
print ("Enter a number.enter 0 when finished")
enterednum <-as.integer(readLines(con=stdin(),1))
finalnum = 0
while (enterednum != 0)
print ("Enter another number. enter 0 when finished");
newnum <-as.integer(readLines(con=stdin(),1));
finalnum <- (enterednum + newnum)
print(paste("The sum of your numbers is", finalnum,"."))
The point of the exercise is to use while statements. While the false condition of the While statement (entering "0") works, any time the initial input is anything but 0, I receive a debug error for any lines after the while statement. Been wracking my brain and digging through here, but not been able to figure it out. Any help is appreciated.
r
check out the answers here: stackoverflow.com/questions/44012056/…
– GordonShumway
Nov 9 at 4:51
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
still in the learning stages of R. Trying to setup a basic piece of code that will allow me to have a user enter numbers until they enter "0", at which time the program will sum entries and display to the user. This is what I have so far:
print ("Enter a number.enter 0 when finished")
enterednum <-as.integer(readLines(con=stdin(),1))
finalnum = 0
while (enterednum != 0)
print ("Enter another number. enter 0 when finished");
newnum <-as.integer(readLines(con=stdin(),1));
finalnum <- (enterednum + newnum)
print(paste("The sum of your numbers is", finalnum,"."))
The point of the exercise is to use while statements. While the false condition of the While statement (entering "0") works, any time the initial input is anything but 0, I receive a debug error for any lines after the while statement. Been wracking my brain and digging through here, but not been able to figure it out. Any help is appreciated.
r
still in the learning stages of R. Trying to setup a basic piece of code that will allow me to have a user enter numbers until they enter "0", at which time the program will sum entries and display to the user. This is what I have so far:
print ("Enter a number.enter 0 when finished")
enterednum <-as.integer(readLines(con=stdin(),1))
finalnum = 0
while (enterednum != 0)
print ("Enter another number. enter 0 when finished");
newnum <-as.integer(readLines(con=stdin(),1));
finalnum <- (enterednum + newnum)
print(paste("The sum of your numbers is", finalnum,"."))
The point of the exercise is to use while statements. While the false condition of the While statement (entering "0") works, any time the initial input is anything but 0, I receive a debug error for any lines after the while statement. Been wracking my brain and digging through here, but not been able to figure it out. Any help is appreciated.
r
r
asked Nov 9 at 4:30
A Nico
31
31
check out the answers here: stackoverflow.com/questions/44012056/…
– GordonShumway
Nov 9 at 4:51
add a comment |
check out the answers here: stackoverflow.com/questions/44012056/…
– GordonShumway
Nov 9 at 4:51
check out the answers here: stackoverflow.com/questions/44012056/…
– GordonShumway
Nov 9 at 4:51
check out the answers here: stackoverflow.com/questions/44012056/…
– GordonShumway
Nov 9 at 4:51
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
Your while loop is based on the condition when enterednum != 0
. However within the while loop you do not update the enterednum
- which meaning it is an infinite loop and would never stop. It would be great if you either change the stoping condition to newnum != 0
or update enterednum
inside the loop.
Hope the above helps.
I'm a klutz...such a simple mistake, and modifying the variables to be able to break the loop worked. Thanks!
– A Nico
Nov 9 at 14:35
add a comment |
up vote
0
down vote
Figured out the issue. Thanks to S. Zhong and GordonShumway for the tips! Corrected, working code below.
print ("Enter a number.enter 0 when finished")
enterednum <-as.integer(readLines(con=stdin(),1))
finalnum <- 0
while (enterednum != 0)
finalnum = (finalnum + enterednum)
print ("Enter another number. enter 0 when finished");
enterednum <-as.integer(readLines(con=stdin(),1));
print(paste("The sum of your numbers is", finalnum,"."))
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
Your while loop is based on the condition when enterednum != 0
. However within the while loop you do not update the enterednum
- which meaning it is an infinite loop and would never stop. It would be great if you either change the stoping condition to newnum != 0
or update enterednum
inside the loop.
Hope the above helps.
I'm a klutz...such a simple mistake, and modifying the variables to be able to break the loop worked. Thanks!
– A Nico
Nov 9 at 14:35
add a comment |
up vote
0
down vote
accepted
Your while loop is based on the condition when enterednum != 0
. However within the while loop you do not update the enterednum
- which meaning it is an infinite loop and would never stop. It would be great if you either change the stoping condition to newnum != 0
or update enterednum
inside the loop.
Hope the above helps.
I'm a klutz...such a simple mistake, and modifying the variables to be able to break the loop worked. Thanks!
– A Nico
Nov 9 at 14:35
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
Your while loop is based on the condition when enterednum != 0
. However within the while loop you do not update the enterednum
- which meaning it is an infinite loop and would never stop. It would be great if you either change the stoping condition to newnum != 0
or update enterednum
inside the loop.
Hope the above helps.
Your while loop is based on the condition when enterednum != 0
. However within the while loop you do not update the enterednum
- which meaning it is an infinite loop and would never stop. It would be great if you either change the stoping condition to newnum != 0
or update enterednum
inside the loop.
Hope the above helps.
answered Nov 9 at 7:04
S.Zhong
263
263
I'm a klutz...such a simple mistake, and modifying the variables to be able to break the loop worked. Thanks!
– A Nico
Nov 9 at 14:35
add a comment |
I'm a klutz...such a simple mistake, and modifying the variables to be able to break the loop worked. Thanks!
– A Nico
Nov 9 at 14:35
I'm a klutz...such a simple mistake, and modifying the variables to be able to break the loop worked. Thanks!
– A Nico
Nov 9 at 14:35
I'm a klutz...such a simple mistake, and modifying the variables to be able to break the loop worked. Thanks!
– A Nico
Nov 9 at 14:35
add a comment |
up vote
0
down vote
Figured out the issue. Thanks to S. Zhong and GordonShumway for the tips! Corrected, working code below.
print ("Enter a number.enter 0 when finished")
enterednum <-as.integer(readLines(con=stdin(),1))
finalnum <- 0
while (enterednum != 0)
finalnum = (finalnum + enterednum)
print ("Enter another number. enter 0 when finished");
enterednum <-as.integer(readLines(con=stdin(),1));
print(paste("The sum of your numbers is", finalnum,"."))
add a comment |
up vote
0
down vote
Figured out the issue. Thanks to S. Zhong and GordonShumway for the tips! Corrected, working code below.
print ("Enter a number.enter 0 when finished")
enterednum <-as.integer(readLines(con=stdin(),1))
finalnum <- 0
while (enterednum != 0)
finalnum = (finalnum + enterednum)
print ("Enter another number. enter 0 when finished");
enterednum <-as.integer(readLines(con=stdin(),1));
print(paste("The sum of your numbers is", finalnum,"."))
add a comment |
up vote
0
down vote
up vote
0
down vote
Figured out the issue. Thanks to S. Zhong and GordonShumway for the tips! Corrected, working code below.
print ("Enter a number.enter 0 when finished")
enterednum <-as.integer(readLines(con=stdin(),1))
finalnum <- 0
while (enterednum != 0)
finalnum = (finalnum + enterednum)
print ("Enter another number. enter 0 when finished");
enterednum <-as.integer(readLines(con=stdin(),1));
print(paste("The sum of your numbers is", finalnum,"."))
Figured out the issue. Thanks to S. Zhong and GordonShumway for the tips! Corrected, working code below.
print ("Enter a number.enter 0 when finished")
enterednum <-as.integer(readLines(con=stdin(),1))
finalnum <- 0
while (enterednum != 0)
finalnum = (finalnum + enterednum)
print ("Enter another number. enter 0 when finished");
enterednum <-as.integer(readLines(con=stdin(),1));
print(paste("The sum of your numbers is", finalnum,"."))
answered Nov 9 at 14:38
A Nico
31
31
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219900%2fbasic-r-question-updatable-input-in-while-loop%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
check out the answers here: stackoverflow.com/questions/44012056/…
– GordonShumway
Nov 9 at 4:51