When I input 3 even numbers it shows that I input 4
Clash 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.
c++
New contributor
add a comment |Â
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.
c++
New contributor
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
add a comment |Â
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.
c++
New contributor
#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
New contributor
edited 17 mins ago
John Topley
83.7k40180228
83.7k40180228
New contributor
asked 3 hours ago
Rendell Joseph T. Perdito
433
433
New contributor
New contributor
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
add a comment |Â
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
add a comment |Â
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) ...
Really, the loop should bewhile ((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
add a comment |Â
up vote
5
down vote
0 % 2 == 0
This is also counted as evenÂ
add a comment |Â
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
add a comment |Â
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) ...
Really, the loop should bewhile ((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
add a comment |Â
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) ...
Really, the loop should bewhile ((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
add a comment |Â
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) ...
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) ...
edited 44 mins ago
answered 3 hours ago
Jean-François Fabre
94.4k848105
94.4k848105
Really, the loop should bewhile ((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
add a comment |Â
Really, the loop should bewhile ((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
add a comment |Â
up vote
5
down vote
0 % 2 == 0
This is also counted as evenÂ
add a comment |Â
up vote
5
down vote
0 % 2 == 0
This is also counted as evenÂ
add a comment |Â
up vote
5
down vote
up vote
5
down vote
0 % 2 == 0
This is also counted as evenÂ
0 % 2 == 0
This is also counted as evenÂ
edited 14 mins ago
user2397282
1,390113373
1,390113373
answered 3 hours ago
kaan bobac
37717
37717
add a comment |Â
add a comment |Â
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
add a comment |Â
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
add a comment |Â
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
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 3 hours ago
Shrikanth N
369110
369110
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-input-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
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