When I input 3 even numbers it shows that I inputted 4

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
7
down vote

favorite












#include <iostream>
using namespace std;

int main()
int odd=0, even=0, value;
cout<<"Enter Numbersn";
cout<<"Enter 0 to Endn";
do

cin>>value;
if (value % 2==0)
even++;
else
odd++;

while (value !=0);

cout<<"The number of odd numbers is: "<<odd<<endl;
cout<<"The number of even numbers is: "<<even;

return 0;



Something is wrong and I need help, when I end the program there is always +1 in even numbers.










share|improve this question









New contributor




Rendell Joseph T. Perdito is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2




    the problem is that when you enter 0 to end the loop it counts as even number...
    – Jean-François Fabre
    54 mins ago






  • 1




    Read How to debug small programs
    – Basile Starynkevitch
    40 mins ago










  • Ideally you should check the exit condition before processing in cases like this.
    – Shrikanth N
    38 mins ago














up vote
7
down vote

favorite












#include <iostream>
using namespace std;

int main()
int odd=0, even=0, value;
cout<<"Enter Numbersn";
cout<<"Enter 0 to Endn";
do

cin>>value;
if (value % 2==0)
even++;
else
odd++;

while (value !=0);

cout<<"The number of odd numbers is: "<<odd<<endl;
cout<<"The number of even numbers is: "<<even;

return 0;



Something is wrong and I need help, when I end the program there is always +1 in even numbers.










share|improve this question









New contributor




Rendell Joseph T. Perdito is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 2




    the problem is that when you enter 0 to end the loop it counts as even number...
    – Jean-François Fabre
    54 mins ago






  • 1




    Read How to debug small programs
    – Basile Starynkevitch
    40 mins ago










  • Ideally you should check the exit condition before processing in cases like this.
    – Shrikanth N
    38 mins ago












up vote
7
down vote

favorite









up vote
7
down vote

favorite











#include <iostream>
using namespace std;

int main()
int odd=0, even=0, value;
cout<<"Enter Numbersn";
cout<<"Enter 0 to Endn";
do

cin>>value;
if (value % 2==0)
even++;
else
odd++;

while (value !=0);

cout<<"The number of odd numbers is: "<<odd<<endl;
cout<<"The number of even numbers is: "<<even;

return 0;



Something is wrong and I need help, when I end the program there is always +1 in even numbers.










share|improve this question









New contributor




Rendell Joseph T. Perdito is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











#include <iostream>
using namespace std;

int main()
int odd=0, even=0, value;
cout<<"Enter Numbersn";
cout<<"Enter 0 to Endn";
do

cin>>value;
if (value % 2==0)
even++;
else
odd++;

while (value !=0);

cout<<"The number of odd numbers is: "<<odd<<endl;
cout<<"The number of even numbers is: "<<even;

return 0;



Something is wrong and I need help, when I end the program there is always +1 in even numbers.







c++






share|improve this question









New contributor




Rendell Joseph T. Perdito is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Rendell Joseph T. Perdito is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited 10 mins ago









Shrikanth N

349110




349110






New contributor




Rendell Joseph T. Perdito is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked 56 mins ago









Rendell Joseph T. Perdito

453




453




New contributor




Rendell Joseph T. Perdito is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Rendell Joseph T. Perdito is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Rendell Joseph T. Perdito is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 2




    the problem is that when you enter 0 to end the loop it counts as even number...
    – Jean-François Fabre
    54 mins ago






  • 1




    Read How to debug small programs
    – Basile Starynkevitch
    40 mins ago










  • Ideally you should check the exit condition before processing in cases like this.
    – Shrikanth N
    38 mins ago












  • 2




    the problem is that when you enter 0 to end the loop it counts as even number...
    – Jean-François Fabre
    54 mins ago






  • 1




    Read How to debug small programs
    – Basile Starynkevitch
    40 mins ago










  • Ideally you should check the exit condition before processing in cases like this.
    – Shrikanth N
    38 mins ago







2




2




the problem is that when you enter 0 to end the loop it counts as even number...
– Jean-François Fabre
54 mins ago




the problem is that when you enter 0 to end the loop it counts as even number...
– Jean-François Fabre
54 mins ago




1




1




Read How to debug small programs
– Basile Starynkevitch
40 mins ago




Read How to debug small programs
– Basile Starynkevitch
40 mins ago












Ideally you should check the exit condition before processing in cases like this.
– Shrikanth N
38 mins ago




Ideally you should check the exit condition before processing in cases like this.
– Shrikanth N
38 mins ago












3 Answers
3






active

oldest

votes

















up vote
10
down vote



accepted










the problem is that when you enter 0 to end the loop it counts it as an even number before exiting the loop...



Break as soon as 0 is entered instead, and use an infinite loop



for (;;)

cin>>value;
if (!value) break; // stop now
if (value % 2==0)
even++;
else
odd++;






share|improve this answer



























    up vote
    3
    down vote













    0 % 2 = 0


    This is also counted as even






    share|improve this answer





























      up vote
      0
      down vote













      Its a good programming practice to check if you have a valid input before processing it.
      A similar approach here of checking the input and then decide if it needs to be processed or not (to check if it an even number or odd) should solve your problem.



      As suggested in one of the answers above, checking for valid input before processing should solve your problem:
      if (!value) break; // stop now






      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: false,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );






        Rendell Joseph T. Perdito is a new contributor. Be nice, and check out our Code of Conduct.









         

        draft saved


        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52817605%2fwhen-i-input-3-even-numbers-it-shows-that-i-inputted-4%23new-answer', 'question_page');

        );

        Post as a guest






























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes








        up vote
        10
        down vote



        accepted










        the problem is that when you enter 0 to end the loop it counts it as an even number before exiting the loop...



        Break as soon as 0 is entered instead, and use an infinite loop



        for (;;)

        cin>>value;
        if (!value) break; // stop now
        if (value % 2==0)
        even++;
        else
        odd++;






        share|improve this answer
























          up vote
          10
          down vote



          accepted










          the problem is that when you enter 0 to end the loop it counts it as an even number before exiting the loop...



          Break as soon as 0 is entered instead, and use an infinite loop



          for (;;)

          cin>>value;
          if (!value) break; // stop now
          if (value % 2==0)
          even++;
          else
          odd++;






          share|improve this answer






















            up vote
            10
            down vote



            accepted







            up vote
            10
            down vote



            accepted






            the problem is that when you enter 0 to end the loop it counts it as an even number before exiting the loop...



            Break as soon as 0 is entered instead, and use an infinite loop



            for (;;)

            cin>>value;
            if (!value) break; // stop now
            if (value % 2==0)
            even++;
            else
            odd++;






            share|improve this answer












            the problem is that when you enter 0 to end the loop it counts it as an even number before exiting the loop...



            Break as soon as 0 is entered instead, and use an infinite loop



            for (;;)

            cin>>value;
            if (!value) break; // stop now
            if (value % 2==0)
            even++;
            else
            odd++;







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered 53 mins ago









            Jean-François Fabre

            94.3k847105




            94.3k847105






















                up vote
                3
                down vote













                0 % 2 = 0


                This is also counted as even






                share|improve this answer


























                  up vote
                  3
                  down vote













                  0 % 2 = 0


                  This is also counted as even






                  share|improve this answer
























                    up vote
                    3
                    down vote










                    up vote
                    3
                    down vote









                    0 % 2 = 0


                    This is also counted as even






                    share|improve this answer














                    0 % 2 = 0


                    This is also counted as even







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited 44 mins ago









                    Suraj Rao

                    21.7k75266




                    21.7k75266










                    answered 48 mins ago









                    kaan bobac

                    35717




                    35717




















                        up vote
                        0
                        down vote













                        Its a good programming practice to check if you have a valid input before processing it.
                        A similar approach here of checking the input and then decide if it needs to be processed or not (to check if it an even number or odd) should solve your problem.



                        As suggested in one of the answers above, checking for valid input before processing should solve your problem:
                        if (!value) break; // stop now






                        share|improve this answer
























                          up vote
                          0
                          down vote













                          Its a good programming practice to check if you have a valid input before processing it.
                          A similar approach here of checking the input and then decide if it needs to be processed or not (to check if it an even number or odd) should solve your problem.



                          As suggested in one of the answers above, checking for valid input before processing should solve your problem:
                          if (!value) break; // stop now






                          share|improve this answer






















                            up vote
                            0
                            down vote










                            up vote
                            0
                            down vote









                            Its a good programming practice to check if you have a valid input before processing it.
                            A similar approach here of checking the input and then decide if it needs to be processed or not (to check if it an even number or odd) should solve your problem.



                            As suggested in one of the answers above, checking for valid input before processing should solve your problem:
                            if (!value) break; // stop now






                            share|improve this answer












                            Its a good programming practice to check if you have a valid input before processing it.
                            A similar approach here of checking the input and then decide if it needs to be processed or not (to check if it an even number or odd) should solve your problem.



                            As suggested in one of the answers above, checking for valid input before processing should solve your problem:
                            if (!value) break; // stop now







                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered 19 mins ago









                            Shrikanth N

                            349110




                            349110




















                                Rendell Joseph T. Perdito is a new contributor. Be nice, and check out our Code of Conduct.









                                 

                                draft saved


                                draft discarded


















                                Rendell Joseph T. Perdito is a new contributor. Be nice, and check out our Code of Conduct.












                                Rendell Joseph T. Perdito is a new contributor. Be nice, and check out our Code of Conduct.











                                Rendell Joseph T. Perdito is a new contributor. Be nice, and check out our Code of Conduct.













                                 


                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52817605%2fwhen-i-input-3-even-numbers-it-shows-that-i-inputted-4%23new-answer', 'question_page');

                                );

                                Post as a guest













































































                                Comments

                                Popular posts from this blog

                                How to decode/decipher Mozilla Firefox proprietary .jsonlz4 format? (sessionstore-backups/recovery.jsonlz4)

                                White Anglo-Saxon Protestant

                                How to be valuable when automation & IT are stealing my job (and maybe my whole career)? [closed]