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

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











up vote
6
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.















  • 6




    Read How to debug small programs
    – Basile Starynkevitch
    3 hours ago






  • 2




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














up vote
6
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.















  • 6




    Read How to debug small programs
    – Basile Starynkevitch
    3 hours ago






  • 2




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












up vote
6
down vote

favorite









up vote
6
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 17 mins ago









John Topley

83.7k40180228




83.7k40180228






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 3 hours ago









Rendell Joseph T. Perdito

433




433




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.







  • 6




    Read How to debug small programs
    – Basile Starynkevitch
    3 hours ago






  • 2




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












  • 6




    Read How to debug small programs
    – Basile Starynkevitch
    3 hours ago






  • 2




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







6




6




Read How to debug small programs
– Basile Starynkevitch
3 hours ago




Read How to debug small programs
– Basile Starynkevitch
3 hours ago




2




2




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




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












3 Answers
3






active

oldest

votes

















up vote
15
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++;



as stated in comments, an alternative would be to use a conditional while loop which short-circuits if the input fails & tests the value against zero. in that case, you don't need to test for zero within the loop:



while ((cin >> value) && value) ... 





share|improve this answer






















  • Really, the loop should be while ((cin >> value) && value)
    – Ben Voigt
    50 mins ago










  • you're right, it's the best way to avoid the infinite loop. I wasn't trying to provide the best answer loop-wise, and I'm very surprised of that much upvotes... I'll edit because your suggestion is excellent.
    – Jean-François Fabre
    45 mins ago

















up vote
5
down vote













0 % 2 == 0


This is also counted as even 






share|improve this answer





























    up vote
    2
    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-input-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
      15
      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++;



      as stated in comments, an alternative would be to use a conditional while loop which short-circuits if the input fails & tests the value against zero. in that case, you don't need to test for zero within the loop:



      while ((cin >> value) && value) ... 





      share|improve this answer






















      • Really, the loop should be while ((cin >> value) && value)
        – Ben Voigt
        50 mins ago










      • you're right, it's the best way to avoid the infinite loop. I wasn't trying to provide the best answer loop-wise, and I'm very surprised of that much upvotes... I'll edit because your suggestion is excellent.
        – Jean-François Fabre
        45 mins ago














      up vote
      15
      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++;



      as stated in comments, an alternative would be to use a conditional while loop which short-circuits if the input fails & tests the value against zero. in that case, you don't need to test for zero within the loop:



      while ((cin >> value) && value) ... 





      share|improve this answer






















      • Really, the loop should be while ((cin >> value) && value)
        – Ben Voigt
        50 mins ago










      • you're right, it's the best way to avoid the infinite loop. I wasn't trying to provide the best answer loop-wise, and I'm very surprised of that much upvotes... I'll edit because your suggestion is excellent.
        – Jean-François Fabre
        45 mins ago












      up vote
      15
      down vote



      accepted







      up vote
      15
      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++;



      as stated in comments, an alternative would be to use a conditional while loop which short-circuits if the input fails & tests the value against zero. in that case, you don't need to test for zero within the loop:



      while ((cin >> value) && value) ... 





      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++;



      as stated in comments, an alternative would be to use a conditional while loop which short-circuits if the input fails & tests the value against zero. in that case, you don't need to test for zero within the loop:



      while ((cin >> value) && value) ... 






      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited 44 mins ago

























      answered 3 hours ago









      Jean-François Fabre

      94.4k848105




      94.4k848105











      • Really, the loop should be while ((cin >> value) && value)
        – Ben Voigt
        50 mins ago










      • you're right, it's the best way to avoid the infinite loop. I wasn't trying to provide the best answer loop-wise, and I'm very surprised of that much upvotes... I'll edit because your suggestion is excellent.
        – Jean-François Fabre
        45 mins ago
















      • Really, the loop should be while ((cin >> value) && value)
        – Ben Voigt
        50 mins ago










      • you're right, it's the best way to avoid the infinite loop. I wasn't trying to provide the best answer loop-wise, and I'm very surprised of that much upvotes... I'll edit because your suggestion is excellent.
        – Jean-François Fabre
        45 mins ago















      Really, the loop should be while ((cin >> value) && value)
      – Ben Voigt
      50 mins ago




      Really, the loop should be while ((cin >> value) && value)
      – Ben Voigt
      50 mins ago












      you're right, it's the best way to avoid the infinite loop. I wasn't trying to provide the best answer loop-wise, and I'm very surprised of that much upvotes... I'll edit because your suggestion is excellent.
      – Jean-François Fabre
      45 mins ago




      you're right, it's the best way to avoid the infinite loop. I wasn't trying to provide the best answer loop-wise, and I'm very surprised of that much upvotes... I'll edit because your suggestion is excellent.
      – Jean-François Fabre
      45 mins ago












      up vote
      5
      down vote













      0 % 2 == 0


      This is also counted as even 






      share|improve this answer


























        up vote
        5
        down vote













        0 % 2 == 0


        This is also counted as even 






        share|improve this answer
























          up vote
          5
          down vote










          up vote
          5
          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 14 mins ago









          user2397282

          1,390113373




          1,390113373










          answered 3 hours ago









          kaan bobac

          37717




          37717




















              up vote
              2
              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
                2
                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
                  2
                  down vote










                  up vote
                  2
                  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 3 hours ago









                  Shrikanth N

                  369110




                  369110




















                      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-input-4%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      Long meetings (6-7 hours a day): Being “babysat” by supervisor

                      Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

                      Confectionery