Can't figure out how to give user all the options for starting, stopping, and restarting program?





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















Ok so I am trying to build this random number teller which basically tells the users whether the number they input is less than, greater than, or equal to 50 and also give them the options to start, stop, and restart the "random number teller" Here is the code:



    #include <iostream>


using namespace std;

main() {
cin >> boolalpha;

int invalid_answer {0};
const int const_num {50};
int random_num {};
char answer {};
int keep_going {};

while (keep_going == 0) {
while (invalid_answer == 0) {

//=======================================================================================================================================

cout << "Enter a random number and we will tell you if it is greater than or less than " << const_num << ": " << endl;
cin >> random_num;

if (random_num > const_num) {

cout << random_num << " is greater than " << const_num;
}
else if (random_num == const_num) {

cout << random_num << " is the same as " << const_num << endl;
}
else {

cout << random_num << " is less than " << const_num << endl;
}

cout << "Want to try again? Type "Y" or "N"";
cin >> answer;

//=======================================================================================================================================

if (answer == 'N') {

cout << "Ok then, sorry to see you miss out" << endl;
keep_going = 1;
}

//=======================================================================================================================================

while(answer == 'Y') {

cout << "Enter a random number and we will tell you if it is greater than or less than " << const_num << ": " << endl;
cin >> random_num;

if (random_num > const_num) {

cout << random_num << " is greater than " << const_num;
}
else if (random_num == const_num) {

cout << random_num << " is the same as " << const_num << endl;
}
else {

cout << random_num << " is less than " << const_num << endl;
}

cout << "nWant to try again? Type "Y" or "N"";
cin >> answer;
}

//=======================================================================================================================================

if (answer != 'Y' || answer != 'N') {
invalid_answer = 1;
}

//=======================================================================================================================================

while (invalid_answer == 1) {
cout << "I'm sorry what? Please note that answers are case sensitive. Answer again: ";
cin >> answer;

if (answer == 'Y') {
invalid_answer = 0;
}
else if (answer == 'N') {
cout << "Ok then, sorry to see you miss out" << endl;
keep_going = 1;
}
}
}
}

}


Whenever I say "N" for No I don't want to redo the random number checker, it doesn't change keep_going to 1 it just moves on to one of the other if or while statements below it. So when you input "N" it just outputs either "Enter a random number and we will tell you if it is greater than or less than " << const_num << ": " or "I'm sorry what? Please note that answers are case sensitive. Answer again: "










share|improve this question























  • Remember that C++ uses short-circuit evaluation for logical expression. With that information, think a little bit about the condition answer != 'Y' || answer != 'N', and what happens if answer is equal to 'N'.

    – Some programmer dude
    Jan 3 at 9:15




















1















Ok so I am trying to build this random number teller which basically tells the users whether the number they input is less than, greater than, or equal to 50 and also give them the options to start, stop, and restart the "random number teller" Here is the code:



    #include <iostream>


using namespace std;

main() {
cin >> boolalpha;

int invalid_answer {0};
const int const_num {50};
int random_num {};
char answer {};
int keep_going {};

while (keep_going == 0) {
while (invalid_answer == 0) {

//=======================================================================================================================================

cout << "Enter a random number and we will tell you if it is greater than or less than " << const_num << ": " << endl;
cin >> random_num;

if (random_num > const_num) {

cout << random_num << " is greater than " << const_num;
}
else if (random_num == const_num) {

cout << random_num << " is the same as " << const_num << endl;
}
else {

cout << random_num << " is less than " << const_num << endl;
}

cout << "Want to try again? Type "Y" or "N"";
cin >> answer;

//=======================================================================================================================================

if (answer == 'N') {

cout << "Ok then, sorry to see you miss out" << endl;
keep_going = 1;
}

//=======================================================================================================================================

while(answer == 'Y') {

cout << "Enter a random number and we will tell you if it is greater than or less than " << const_num << ": " << endl;
cin >> random_num;

if (random_num > const_num) {

cout << random_num << " is greater than " << const_num;
}
else if (random_num == const_num) {

cout << random_num << " is the same as " << const_num << endl;
}
else {

cout << random_num << " is less than " << const_num << endl;
}

cout << "nWant to try again? Type "Y" or "N"";
cin >> answer;
}

//=======================================================================================================================================

if (answer != 'Y' || answer != 'N') {
invalid_answer = 1;
}

//=======================================================================================================================================

while (invalid_answer == 1) {
cout << "I'm sorry what? Please note that answers are case sensitive. Answer again: ";
cin >> answer;

if (answer == 'Y') {
invalid_answer = 0;
}
else if (answer == 'N') {
cout << "Ok then, sorry to see you miss out" << endl;
keep_going = 1;
}
}
}
}

}


Whenever I say "N" for No I don't want to redo the random number checker, it doesn't change keep_going to 1 it just moves on to one of the other if or while statements below it. So when you input "N" it just outputs either "Enter a random number and we will tell you if it is greater than or less than " << const_num << ": " or "I'm sorry what? Please note that answers are case sensitive. Answer again: "










share|improve this question























  • Remember that C++ uses short-circuit evaluation for logical expression. With that information, think a little bit about the condition answer != 'Y' || answer != 'N', and what happens if answer is equal to 'N'.

    – Some programmer dude
    Jan 3 at 9:15
















1












1








1








Ok so I am trying to build this random number teller which basically tells the users whether the number they input is less than, greater than, or equal to 50 and also give them the options to start, stop, and restart the "random number teller" Here is the code:



    #include <iostream>


using namespace std;

main() {
cin >> boolalpha;

int invalid_answer {0};
const int const_num {50};
int random_num {};
char answer {};
int keep_going {};

while (keep_going == 0) {
while (invalid_answer == 0) {

//=======================================================================================================================================

cout << "Enter a random number and we will tell you if it is greater than or less than " << const_num << ": " << endl;
cin >> random_num;

if (random_num > const_num) {

cout << random_num << " is greater than " << const_num;
}
else if (random_num == const_num) {

cout << random_num << " is the same as " << const_num << endl;
}
else {

cout << random_num << " is less than " << const_num << endl;
}

cout << "Want to try again? Type "Y" or "N"";
cin >> answer;

//=======================================================================================================================================

if (answer == 'N') {

cout << "Ok then, sorry to see you miss out" << endl;
keep_going = 1;
}

//=======================================================================================================================================

while(answer == 'Y') {

cout << "Enter a random number and we will tell you if it is greater than or less than " << const_num << ": " << endl;
cin >> random_num;

if (random_num > const_num) {

cout << random_num << " is greater than " << const_num;
}
else if (random_num == const_num) {

cout << random_num << " is the same as " << const_num << endl;
}
else {

cout << random_num << " is less than " << const_num << endl;
}

cout << "nWant to try again? Type "Y" or "N"";
cin >> answer;
}

//=======================================================================================================================================

if (answer != 'Y' || answer != 'N') {
invalid_answer = 1;
}

//=======================================================================================================================================

while (invalid_answer == 1) {
cout << "I'm sorry what? Please note that answers are case sensitive. Answer again: ";
cin >> answer;

if (answer == 'Y') {
invalid_answer = 0;
}
else if (answer == 'N') {
cout << "Ok then, sorry to see you miss out" << endl;
keep_going = 1;
}
}
}
}

}


Whenever I say "N" for No I don't want to redo the random number checker, it doesn't change keep_going to 1 it just moves on to one of the other if or while statements below it. So when you input "N" it just outputs either "Enter a random number and we will tell you if it is greater than or less than " << const_num << ": " or "I'm sorry what? Please note that answers are case sensitive. Answer again: "










share|improve this question














Ok so I am trying to build this random number teller which basically tells the users whether the number they input is less than, greater than, or equal to 50 and also give them the options to start, stop, and restart the "random number teller" Here is the code:



    #include <iostream>


using namespace std;

main() {
cin >> boolalpha;

int invalid_answer {0};
const int const_num {50};
int random_num {};
char answer {};
int keep_going {};

while (keep_going == 0) {
while (invalid_answer == 0) {

//=======================================================================================================================================

cout << "Enter a random number and we will tell you if it is greater than or less than " << const_num << ": " << endl;
cin >> random_num;

if (random_num > const_num) {

cout << random_num << " is greater than " << const_num;
}
else if (random_num == const_num) {

cout << random_num << " is the same as " << const_num << endl;
}
else {

cout << random_num << " is less than " << const_num << endl;
}

cout << "Want to try again? Type "Y" or "N"";
cin >> answer;

//=======================================================================================================================================

if (answer == 'N') {

cout << "Ok then, sorry to see you miss out" << endl;
keep_going = 1;
}

//=======================================================================================================================================

while(answer == 'Y') {

cout << "Enter a random number and we will tell you if it is greater than or less than " << const_num << ": " << endl;
cin >> random_num;

if (random_num > const_num) {

cout << random_num << " is greater than " << const_num;
}
else if (random_num == const_num) {

cout << random_num << " is the same as " << const_num << endl;
}
else {

cout << random_num << " is less than " << const_num << endl;
}

cout << "nWant to try again? Type "Y" or "N"";
cin >> answer;
}

//=======================================================================================================================================

if (answer != 'Y' || answer != 'N') {
invalid_answer = 1;
}

//=======================================================================================================================================

while (invalid_answer == 1) {
cout << "I'm sorry what? Please note that answers are case sensitive. Answer again: ";
cin >> answer;

if (answer == 'Y') {
invalid_answer = 0;
}
else if (answer == 'N') {
cout << "Ok then, sorry to see you miss out" << endl;
keep_going = 1;
}
}
}
}

}


Whenever I say "N" for No I don't want to redo the random number checker, it doesn't change keep_going to 1 it just moves on to one of the other if or while statements below it. So when you input "N" it just outputs either "Enter a random number and we will tell you if it is greater than or less than " << const_num << ": " or "I'm sorry what? Please note that answers are case sensitive. Answer again: "







c++






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 3 at 9:10









ParkerParker

83




83













  • Remember that C++ uses short-circuit evaluation for logical expression. With that information, think a little bit about the condition answer != 'Y' || answer != 'N', and what happens if answer is equal to 'N'.

    – Some programmer dude
    Jan 3 at 9:15





















  • Remember that C++ uses short-circuit evaluation for logical expression. With that information, think a little bit about the condition answer != 'Y' || answer != 'N', and what happens if answer is equal to 'N'.

    – Some programmer dude
    Jan 3 at 9:15



















Remember that C++ uses short-circuit evaluation for logical expression. With that information, think a little bit about the condition answer != 'Y' || answer != 'N', and what happens if answer is equal to 'N'.

– Some programmer dude
Jan 3 at 9:15







Remember that C++ uses short-circuit evaluation for logical expression. With that information, think a little bit about the condition answer != 'Y' || answer != 'N', and what happens if answer is equal to 'N'.

– Some programmer dude
Jan 3 at 9:15














2 Answers
2






active

oldest

votes


















1














The problem is with this bit of code:



   if (answer != 'Y' || answer != 'N') {
invalid_answer = 1;
}


When answer is 'N', answer != 'Y' is true and invalid_answer is set to 1 (because of short-circuit evaluation the rhs of the logical OR is not even evaluated - see quote below).

So the execution will enter the while



while (invalid_answer == 1)


and will print the statements.



You can correct this by:



if (answer == 'Y' || answer == 'N') { //if input is either 'Y' or 'N'
invalid_answer = 0;
}
else { //for all other inputs
invalid_answer = 1;
}



Builtin operators && and || perform short-circuit evaluation (do not evaluate the second operand if the result is known after evaluating the first), but overloaded operators behave like regular function calls and always evaluate both operands




Also note that main should have the type int.






share|improve this answer


























  • Short-circuit evaluation has no effect on the result here. Every character is either not Y or not N.

    – David Schwartz
    Jan 3 at 9:33











  • Yes. The OP was asking for the specific case of 'N'. He must have thought that if input is 'N', the if would fail and the invalid_answer would not be 1. I will update the answer and clarify.

    – P.W
    Jan 3 at 9:36



















0














I figured it out right after I posted the question haha, basically the answer above was correct so I had to split that if statement into 2 others, in which I added an else statement to each also that said invalid_answer = 0; to make sure. But then after the user's second time using the program, if they wanted to quit it wouldn't let them and would just restart it again. I solved that by adding



if (answer == 'N') {

cout << "Ok then, sorry to see you miss out" << endl;
keep_going = 1;
}`


to the bottom of the while(answer == 'Y') loop.






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',
    autoActivateHeartbeat: false,
    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%2f54019204%2fcant-figure-out-how-to-give-user-all-the-options-for-starting-stopping-and-re%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









    1














    The problem is with this bit of code:



       if (answer != 'Y' || answer != 'N') {
    invalid_answer = 1;
    }


    When answer is 'N', answer != 'Y' is true and invalid_answer is set to 1 (because of short-circuit evaluation the rhs of the logical OR is not even evaluated - see quote below).

    So the execution will enter the while



    while (invalid_answer == 1)


    and will print the statements.



    You can correct this by:



    if (answer == 'Y' || answer == 'N') { //if input is either 'Y' or 'N'
    invalid_answer = 0;
    }
    else { //for all other inputs
    invalid_answer = 1;
    }



    Builtin operators && and || perform short-circuit evaluation (do not evaluate the second operand if the result is known after evaluating the first), but overloaded operators behave like regular function calls and always evaluate both operands




    Also note that main should have the type int.






    share|improve this answer


























    • Short-circuit evaluation has no effect on the result here. Every character is either not Y or not N.

      – David Schwartz
      Jan 3 at 9:33











    • Yes. The OP was asking for the specific case of 'N'. He must have thought that if input is 'N', the if would fail and the invalid_answer would not be 1. I will update the answer and clarify.

      – P.W
      Jan 3 at 9:36
















    1














    The problem is with this bit of code:



       if (answer != 'Y' || answer != 'N') {
    invalid_answer = 1;
    }


    When answer is 'N', answer != 'Y' is true and invalid_answer is set to 1 (because of short-circuit evaluation the rhs of the logical OR is not even evaluated - see quote below).

    So the execution will enter the while



    while (invalid_answer == 1)


    and will print the statements.



    You can correct this by:



    if (answer == 'Y' || answer == 'N') { //if input is either 'Y' or 'N'
    invalid_answer = 0;
    }
    else { //for all other inputs
    invalid_answer = 1;
    }



    Builtin operators && and || perform short-circuit evaluation (do not evaluate the second operand if the result is known after evaluating the first), but overloaded operators behave like regular function calls and always evaluate both operands




    Also note that main should have the type int.






    share|improve this answer


























    • Short-circuit evaluation has no effect on the result here. Every character is either not Y or not N.

      – David Schwartz
      Jan 3 at 9:33











    • Yes. The OP was asking for the specific case of 'N'. He must have thought that if input is 'N', the if would fail and the invalid_answer would not be 1. I will update the answer and clarify.

      – P.W
      Jan 3 at 9:36














    1












    1








    1







    The problem is with this bit of code:



       if (answer != 'Y' || answer != 'N') {
    invalid_answer = 1;
    }


    When answer is 'N', answer != 'Y' is true and invalid_answer is set to 1 (because of short-circuit evaluation the rhs of the logical OR is not even evaluated - see quote below).

    So the execution will enter the while



    while (invalid_answer == 1)


    and will print the statements.



    You can correct this by:



    if (answer == 'Y' || answer == 'N') { //if input is either 'Y' or 'N'
    invalid_answer = 0;
    }
    else { //for all other inputs
    invalid_answer = 1;
    }



    Builtin operators && and || perform short-circuit evaluation (do not evaluate the second operand if the result is known after evaluating the first), but overloaded operators behave like regular function calls and always evaluate both operands




    Also note that main should have the type int.






    share|improve this answer















    The problem is with this bit of code:



       if (answer != 'Y' || answer != 'N') {
    invalid_answer = 1;
    }


    When answer is 'N', answer != 'Y' is true and invalid_answer is set to 1 (because of short-circuit evaluation the rhs of the logical OR is not even evaluated - see quote below).

    So the execution will enter the while



    while (invalid_answer == 1)


    and will print the statements.



    You can correct this by:



    if (answer == 'Y' || answer == 'N') { //if input is either 'Y' or 'N'
    invalid_answer = 0;
    }
    else { //for all other inputs
    invalid_answer = 1;
    }



    Builtin operators && and || perform short-circuit evaluation (do not evaluate the second operand if the result is known after evaluating the first), but overloaded operators behave like regular function calls and always evaluate both operands




    Also note that main should have the type int.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Jan 3 at 10:33

























    answered Jan 3 at 9:19









    P.WP.W

    18.4k41758




    18.4k41758













    • Short-circuit evaluation has no effect on the result here. Every character is either not Y or not N.

      – David Schwartz
      Jan 3 at 9:33











    • Yes. The OP was asking for the specific case of 'N'. He must have thought that if input is 'N', the if would fail and the invalid_answer would not be 1. I will update the answer and clarify.

      – P.W
      Jan 3 at 9:36



















    • Short-circuit evaluation has no effect on the result here. Every character is either not Y or not N.

      – David Schwartz
      Jan 3 at 9:33











    • Yes. The OP was asking for the specific case of 'N'. He must have thought that if input is 'N', the if would fail and the invalid_answer would not be 1. I will update the answer and clarify.

      – P.W
      Jan 3 at 9:36

















    Short-circuit evaluation has no effect on the result here. Every character is either not Y or not N.

    – David Schwartz
    Jan 3 at 9:33





    Short-circuit evaluation has no effect on the result here. Every character is either not Y or not N.

    – David Schwartz
    Jan 3 at 9:33













    Yes. The OP was asking for the specific case of 'N'. He must have thought that if input is 'N', the if would fail and the invalid_answer would not be 1. I will update the answer and clarify.

    – P.W
    Jan 3 at 9:36





    Yes. The OP was asking for the specific case of 'N'. He must have thought that if input is 'N', the if would fail and the invalid_answer would not be 1. I will update the answer and clarify.

    – P.W
    Jan 3 at 9:36













    0














    I figured it out right after I posted the question haha, basically the answer above was correct so I had to split that if statement into 2 others, in which I added an else statement to each also that said invalid_answer = 0; to make sure. But then after the user's second time using the program, if they wanted to quit it wouldn't let them and would just restart it again. I solved that by adding



    if (answer == 'N') {

    cout << "Ok then, sorry to see you miss out" << endl;
    keep_going = 1;
    }`


    to the bottom of the while(answer == 'Y') loop.






    share|improve this answer




























      0














      I figured it out right after I posted the question haha, basically the answer above was correct so I had to split that if statement into 2 others, in which I added an else statement to each also that said invalid_answer = 0; to make sure. But then after the user's second time using the program, if they wanted to quit it wouldn't let them and would just restart it again. I solved that by adding



      if (answer == 'N') {

      cout << "Ok then, sorry to see you miss out" << endl;
      keep_going = 1;
      }`


      to the bottom of the while(answer == 'Y') loop.






      share|improve this answer


























        0












        0








        0







        I figured it out right after I posted the question haha, basically the answer above was correct so I had to split that if statement into 2 others, in which I added an else statement to each also that said invalid_answer = 0; to make sure. But then after the user's second time using the program, if they wanted to quit it wouldn't let them and would just restart it again. I solved that by adding



        if (answer == 'N') {

        cout << "Ok then, sorry to see you miss out" << endl;
        keep_going = 1;
        }`


        to the bottom of the while(answer == 'Y') loop.






        share|improve this answer













        I figured it out right after I posted the question haha, basically the answer above was correct so I had to split that if statement into 2 others, in which I added an else statement to each also that said invalid_answer = 0; to make sure. But then after the user's second time using the program, if they wanted to quit it wouldn't let them and would just restart it again. I solved that by adding



        if (answer == 'N') {

        cout << "Ok then, sorry to see you miss out" << endl;
        keep_going = 1;
        }`


        to the bottom of the while(answer == 'Y') loop.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 3 at 9:51









        ParkerParker

        83




        83






























            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54019204%2fcant-figure-out-how-to-give-user-all-the-options-for-starting-stopping-and-re%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







            Popular posts from this blog

            Can a sorcerer learn a 5th-level spell early by creating spell slots using the Font of Magic feature?

            Does disintegrating a polymorphed enemy still kill it after the 2018 errata?

            A Topological Invariant for $pi_3(U(n))$