If the user enter “q”, program quits












1














I have this C++ code and I am trying to do the following:



Prompt the user to enter "p" to play or "q" to quit, if the user enters anything "p" the program will continue, if the user enters "q" program would just terminate and if they entered an invalid input, it would also terminate. How do I do that?.



Thank you,



Here is the code:



#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int Umain = 0;
double Atemp = 0;
double Utemp = 0;
double Working = 0;
double Total = 0;
char Answer = 'x';

void displayOverview ();

void playOrQuit();

void promptNumber();

int main(){

displayOverview();

playOrQuit();

promptNumber();

return 0;
}

void displayOverview(){


}

void playOrQuit(){

string playOrNot;

cout << "If you want to play please press 'p' for play, and 'q' if you wish to quitn";
cin >> playOrNot;

if(playOrNot == "p"){
cout << "Awesome, lets start playing !!! n";

}if(playOrNot == "q"){
cout << "Alright then, see you soon !!n";

}
}


void promptNumber(){

do{
cout << "Please Enter numbers between 1 and 12: ";
cin >> Umain;
cout << "n";
for (Utemp = Umain; Utemp > 0; Utemp--)
{
cout << "Please enter a number: ";
cin >> Atemp;
Working = (Working + Atemp);
}
}while (Answer == 'y');
}









share|improve this question




















  • 3




    And your question is?
    – NathanOliver
    Nov 20 at 22:30










  • Please don't tag your questions in the title - that's what the actual tags are for
    – xaxxon
    Nov 20 at 22:31










  • @NathanOliver How do I do it?
    – Epic Dehaan
    Nov 20 at 22:33










  • if void playOrQuit(); was bool playOrQuit(); you could if (!playOrQuit()) { return 0; }. You're going to want t loop in main or the whole idea is moot.
    – user4581301
    Nov 20 at 22:54


















1














I have this C++ code and I am trying to do the following:



Prompt the user to enter "p" to play or "q" to quit, if the user enters anything "p" the program will continue, if the user enters "q" program would just terminate and if they entered an invalid input, it would also terminate. How do I do that?.



Thank you,



Here is the code:



#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int Umain = 0;
double Atemp = 0;
double Utemp = 0;
double Working = 0;
double Total = 0;
char Answer = 'x';

void displayOverview ();

void playOrQuit();

void promptNumber();

int main(){

displayOverview();

playOrQuit();

promptNumber();

return 0;
}

void displayOverview(){


}

void playOrQuit(){

string playOrNot;

cout << "If you want to play please press 'p' for play, and 'q' if you wish to quitn";
cin >> playOrNot;

if(playOrNot == "p"){
cout << "Awesome, lets start playing !!! n";

}if(playOrNot == "q"){
cout << "Alright then, see you soon !!n";

}
}


void promptNumber(){

do{
cout << "Please Enter numbers between 1 and 12: ";
cin >> Umain;
cout << "n";
for (Utemp = Umain; Utemp > 0; Utemp--)
{
cout << "Please enter a number: ";
cin >> Atemp;
Working = (Working + Atemp);
}
}while (Answer == 'y');
}









share|improve this question




















  • 3




    And your question is?
    – NathanOliver
    Nov 20 at 22:30










  • Please don't tag your questions in the title - that's what the actual tags are for
    – xaxxon
    Nov 20 at 22:31










  • @NathanOliver How do I do it?
    – Epic Dehaan
    Nov 20 at 22:33










  • if void playOrQuit(); was bool playOrQuit(); you could if (!playOrQuit()) { return 0; }. You're going to want t loop in main or the whole idea is moot.
    – user4581301
    Nov 20 at 22:54
















1












1








1







I have this C++ code and I am trying to do the following:



Prompt the user to enter "p" to play or "q" to quit, if the user enters anything "p" the program will continue, if the user enters "q" program would just terminate and if they entered an invalid input, it would also terminate. How do I do that?.



Thank you,



Here is the code:



#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int Umain = 0;
double Atemp = 0;
double Utemp = 0;
double Working = 0;
double Total = 0;
char Answer = 'x';

void displayOverview ();

void playOrQuit();

void promptNumber();

int main(){

displayOverview();

playOrQuit();

promptNumber();

return 0;
}

void displayOverview(){


}

void playOrQuit(){

string playOrNot;

cout << "If you want to play please press 'p' for play, and 'q' if you wish to quitn";
cin >> playOrNot;

if(playOrNot == "p"){
cout << "Awesome, lets start playing !!! n";

}if(playOrNot == "q"){
cout << "Alright then, see you soon !!n";

}
}


void promptNumber(){

do{
cout << "Please Enter numbers between 1 and 12: ";
cin >> Umain;
cout << "n";
for (Utemp = Umain; Utemp > 0; Utemp--)
{
cout << "Please enter a number: ";
cin >> Atemp;
Working = (Working + Atemp);
}
}while (Answer == 'y');
}









share|improve this question















I have this C++ code and I am trying to do the following:



Prompt the user to enter "p" to play or "q" to quit, if the user enters anything "p" the program will continue, if the user enters "q" program would just terminate and if they entered an invalid input, it would also terminate. How do I do that?.



Thank you,



Here is the code:



#include <iostream>
#include <cstdio>
#include <cmath>

using namespace std;

int Umain = 0;
double Atemp = 0;
double Utemp = 0;
double Working = 0;
double Total = 0;
char Answer = 'x';

void displayOverview ();

void playOrQuit();

void promptNumber();

int main(){

displayOverview();

playOrQuit();

promptNumber();

return 0;
}

void displayOverview(){


}

void playOrQuit(){

string playOrNot;

cout << "If you want to play please press 'p' for play, and 'q' if you wish to quitn";
cin >> playOrNot;

if(playOrNot == "p"){
cout << "Awesome, lets start playing !!! n";

}if(playOrNot == "q"){
cout << "Alright then, see you soon !!n";

}
}


void promptNumber(){

do{
cout << "Please Enter numbers between 1 and 12: ";
cin >> Umain;
cout << "n";
for (Utemp = Umain; Utemp > 0; Utemp--)
{
cout << "Please enter a number: ";
cin >> Atemp;
Working = (Working + Atemp);
}
}while (Answer == 'y');
}






c++






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 22:32

























asked Nov 20 at 22:30









Epic Dehaan

335




335








  • 3




    And your question is?
    – NathanOliver
    Nov 20 at 22:30










  • Please don't tag your questions in the title - that's what the actual tags are for
    – xaxxon
    Nov 20 at 22:31










  • @NathanOliver How do I do it?
    – Epic Dehaan
    Nov 20 at 22:33










  • if void playOrQuit(); was bool playOrQuit(); you could if (!playOrQuit()) { return 0; }. You're going to want t loop in main or the whole idea is moot.
    – user4581301
    Nov 20 at 22:54
















  • 3




    And your question is?
    – NathanOliver
    Nov 20 at 22:30










  • Please don't tag your questions in the title - that's what the actual tags are for
    – xaxxon
    Nov 20 at 22:31










  • @NathanOliver How do I do it?
    – Epic Dehaan
    Nov 20 at 22:33










  • if void playOrQuit(); was bool playOrQuit(); you could if (!playOrQuit()) { return 0; }. You're going to want t loop in main or the whole idea is moot.
    – user4581301
    Nov 20 at 22:54










3




3




And your question is?
– NathanOliver
Nov 20 at 22:30




And your question is?
– NathanOliver
Nov 20 at 22:30












Please don't tag your questions in the title - that's what the actual tags are for
– xaxxon
Nov 20 at 22:31




Please don't tag your questions in the title - that's what the actual tags are for
– xaxxon
Nov 20 at 22:31












@NathanOliver How do I do it?
– Epic Dehaan
Nov 20 at 22:33




@NathanOliver How do I do it?
– Epic Dehaan
Nov 20 at 22:33












if void playOrQuit(); was bool playOrQuit(); you could if (!playOrQuit()) { return 0; }. You're going to want t loop in main or the whole idea is moot.
– user4581301
Nov 20 at 22:54






if void playOrQuit(); was bool playOrQuit(); you could if (!playOrQuit()) { return 0; }. You're going to want t loop in main or the whole idea is moot.
– user4581301
Nov 20 at 22:54














4 Answers
4






active

oldest

votes


















4














Just add a call to exit after you detect 'q' was pressed:



}if(playOrNot == "q"){
cout << "Alright then, see you soon !!n";
exit(0); // <=== Add this here


Exiting with a 0 traditionally means the program exited in an expected fashion and without any errors.






share|improve this answer





























    3














    The usual way to do this kind of thing is to have PlayOrQuit return a bool with true meaning "keep on playing" and false meaning "quit". Use that function to control a loop:



    while (PlayOrQuit()) {
    // game logic goes here
    }


    That way you can put any appropriate cleanup code after the game loop instead of having a brute-force exit from down inside the function.






    share|improve this answer





























      1














      There are a couple of ways you can achieve this.
      But I suggest you include the stdlib.h library and use system("exit") right inside your else statements that is meant to exit the program.






      share|improve this answer

















      • 1




        system("exit") is quite a bit heavier in weight than you want or need and lacks portability. It is starting another process to call the exit ms-dos command which will end the program with all the subtlety if a headsman's axe.
        – user4581301
        Nov 20 at 22:52





















      0














      Add end(), return 0 or exit(0).
      Use u brain like, if you need this then i will remember nearest possible thing you spot from past.



      So you never made these kind of mistake.



      }if(playOrNot == "q"){
      cout << "Alright then, see you soon !!n";
      exit(0);
      }





      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%2f53402554%2fif-the-user-enter-q-program-quits%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        4














        Just add a call to exit after you detect 'q' was pressed:



        }if(playOrNot == "q"){
        cout << "Alright then, see you soon !!n";
        exit(0); // <=== Add this here


        Exiting with a 0 traditionally means the program exited in an expected fashion and without any errors.






        share|improve this answer


























          4














          Just add a call to exit after you detect 'q' was pressed:



          }if(playOrNot == "q"){
          cout << "Alright then, see you soon !!n";
          exit(0); // <=== Add this here


          Exiting with a 0 traditionally means the program exited in an expected fashion and without any errors.






          share|improve this answer
























            4












            4








            4






            Just add a call to exit after you detect 'q' was pressed:



            }if(playOrNot == "q"){
            cout << "Alright then, see you soon !!n";
            exit(0); // <=== Add this here


            Exiting with a 0 traditionally means the program exited in an expected fashion and without any errors.






            share|improve this answer












            Just add a call to exit after you detect 'q' was pressed:



            }if(playOrNot == "q"){
            cout << "Alright then, see you soon !!n";
            exit(0); // <=== Add this here


            Exiting with a 0 traditionally means the program exited in an expected fashion and without any errors.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 20 at 22:37









            xaxxon

            14.3k43059




            14.3k43059

























                3














                The usual way to do this kind of thing is to have PlayOrQuit return a bool with true meaning "keep on playing" and false meaning "quit". Use that function to control a loop:



                while (PlayOrQuit()) {
                // game logic goes here
                }


                That way you can put any appropriate cleanup code after the game loop instead of having a brute-force exit from down inside the function.






                share|improve this answer


























                  3














                  The usual way to do this kind of thing is to have PlayOrQuit return a bool with true meaning "keep on playing" and false meaning "quit". Use that function to control a loop:



                  while (PlayOrQuit()) {
                  // game logic goes here
                  }


                  That way you can put any appropriate cleanup code after the game loop instead of having a brute-force exit from down inside the function.






                  share|improve this answer
























                    3












                    3








                    3






                    The usual way to do this kind of thing is to have PlayOrQuit return a bool with true meaning "keep on playing" and false meaning "quit". Use that function to control a loop:



                    while (PlayOrQuit()) {
                    // game logic goes here
                    }


                    That way you can put any appropriate cleanup code after the game loop instead of having a brute-force exit from down inside the function.






                    share|improve this answer












                    The usual way to do this kind of thing is to have PlayOrQuit return a bool with true meaning "keep on playing" and false meaning "quit". Use that function to control a loop:



                    while (PlayOrQuit()) {
                    // game logic goes here
                    }


                    That way you can put any appropriate cleanup code after the game loop instead of having a brute-force exit from down inside the function.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 20 at 22:56









                    Pete Becker

                    56.9k440116




                    56.9k440116























                        1














                        There are a couple of ways you can achieve this.
                        But I suggest you include the stdlib.h library and use system("exit") right inside your else statements that is meant to exit the program.






                        share|improve this answer

















                        • 1




                          system("exit") is quite a bit heavier in weight than you want or need and lacks portability. It is starting another process to call the exit ms-dos command which will end the program with all the subtlety if a headsman's axe.
                          – user4581301
                          Nov 20 at 22:52


















                        1














                        There are a couple of ways you can achieve this.
                        But I suggest you include the stdlib.h library and use system("exit") right inside your else statements that is meant to exit the program.






                        share|improve this answer

















                        • 1




                          system("exit") is quite a bit heavier in weight than you want or need and lacks portability. It is starting another process to call the exit ms-dos command which will end the program with all the subtlety if a headsman's axe.
                          – user4581301
                          Nov 20 at 22:52
















                        1












                        1








                        1






                        There are a couple of ways you can achieve this.
                        But I suggest you include the stdlib.h library and use system("exit") right inside your else statements that is meant to exit the program.






                        share|improve this answer












                        There are a couple of ways you can achieve this.
                        But I suggest you include the stdlib.h library and use system("exit") right inside your else statements that is meant to exit the program.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered Nov 20 at 22:42









                        Beth

                        191




                        191








                        • 1




                          system("exit") is quite a bit heavier in weight than you want or need and lacks portability. It is starting another process to call the exit ms-dos command which will end the program with all the subtlety if a headsman's axe.
                          – user4581301
                          Nov 20 at 22:52
















                        • 1




                          system("exit") is quite a bit heavier in weight than you want or need and lacks portability. It is starting another process to call the exit ms-dos command which will end the program with all the subtlety if a headsman's axe.
                          – user4581301
                          Nov 20 at 22:52










                        1




                        1




                        system("exit") is quite a bit heavier in weight than you want or need and lacks portability. It is starting another process to call the exit ms-dos command which will end the program with all the subtlety if a headsman's axe.
                        – user4581301
                        Nov 20 at 22:52






                        system("exit") is quite a bit heavier in weight than you want or need and lacks portability. It is starting another process to call the exit ms-dos command which will end the program with all the subtlety if a headsman's axe.
                        – user4581301
                        Nov 20 at 22:52













                        0














                        Add end(), return 0 or exit(0).
                        Use u brain like, if you need this then i will remember nearest possible thing you spot from past.



                        So you never made these kind of mistake.



                        }if(playOrNot == "q"){
                        cout << "Alright then, see you soon !!n";
                        exit(0);
                        }





                        share|improve this answer


























                          0














                          Add end(), return 0 or exit(0).
                          Use u brain like, if you need this then i will remember nearest possible thing you spot from past.



                          So you never made these kind of mistake.



                          }if(playOrNot == "q"){
                          cout << "Alright then, see you soon !!n";
                          exit(0);
                          }





                          share|improve this answer
























                            0












                            0








                            0






                            Add end(), return 0 or exit(0).
                            Use u brain like, if you need this then i will remember nearest possible thing you spot from past.



                            So you never made these kind of mistake.



                            }if(playOrNot == "q"){
                            cout << "Alright then, see you soon !!n";
                            exit(0);
                            }





                            share|improve this answer












                            Add end(), return 0 or exit(0).
                            Use u brain like, if you need this then i will remember nearest possible thing you spot from past.



                            So you never made these kind of mistake.



                            }if(playOrNot == "q"){
                            cout << "Alright then, see you soon !!n";
                            exit(0);
                            }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 20 at 23:58









                            Dusan Dusann

                            14




                            14






























                                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%2f53402554%2fif-the-user-enter-q-program-quits%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

                                404 Error Contact Form 7 ajax form submitting

                                How to know if a Active Directory user can login interactively

                                TypeError: fit_transform() missing 1 required positional argument: 'X'