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

Clash 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.
c++
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.
add a comment |Â
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.
c++
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
add a comment |Â
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.
c++
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++
c++
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.
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
add a comment |Â
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
add a comment |Â
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++;
add a comment |Â
up vote
3
down vote
0 % 2 = 0
This is also counted as even
add a comment |Â
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
add a comment |Â
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++;
add a comment |Â
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++;
add a comment |Â
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++;
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++;
answered 53 mins ago
Jean-François Fabre
94.3k847105
94.3k847105
add a comment |Â
add a comment |Â
up vote
3
down vote
0 % 2 = 0
This is also counted as even
add a comment |Â
up vote
3
down vote
0 % 2 = 0
This is also counted as even
add a comment |Â
up vote
3
down vote
up vote
3
down vote
0 % 2 = 0
This is also counted as even
0 % 2 = 0
This is also counted as even
edited 44 mins ago
Suraj Rao
21.7k75266
21.7k75266
answered 48 mins ago
kaan bobac
35717
35717
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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
answered 19 mins ago
Shrikanth N
349110
349110
add a comment |Â
add a comment |Â
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.
Rendell Joseph T. Perdito is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password

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