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.










share|improve this question






















  • check out the answers here: stackoverflow.com/questions/44012056/…
    – GordonShumway
    Nov 9 at 4:51















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.










share|improve this question






















  • check out the answers here: stackoverflow.com/questions/44012056/…
    – GordonShumway
    Nov 9 at 4:51













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.










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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


















  • 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












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.






share|improve this answer





















  • 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




















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,"."))





share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    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.






    share|improve this answer





















    • 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

















    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.






    share|improve this answer





















    • 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















    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.






    share|improve this answer












    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.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    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




















    • 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














    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,"."))





    share|improve this answer

























      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,"."))





      share|improve this answer























        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,"."))





        share|improve this answer












        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,"."))






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 9 at 14:38









        A Nico

        31




        31






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini