Add all even numbers

Feb 12, 2021 at 5:57am
closed account (1Cj3ko23)
Write a program that allows the user to repeatedly enter integer values. Keep both a count and a sum of all even numbers entered. When the user enters 0 or any negative number, report the sum and count of even numbers.

EX:
This is Brent's program to count and add up all the even numbers you
type in. I'll keep track of all the even, non-negative numbers entered.
When you're done, just enter 0 or a negative number to terminate the
program.
Next number? 17
Next number? 72
Next number? 33
Next number? 48
Next number? 21
Next number? 0
2 even numbers were entered for a total sum of 120.
Thanks for playing! Goodbye.
Feb 12, 2021 at 6:25am
So what are you stuck on?

Can you input one number and print it?

Can you loop entering numbers until 0 is entered?

Do you know how to tell an even number from an odd number.

Just dumping your assignment with zero effort isn't likely to get you full code, and it wouldn't help you learn anything either.
Feb 12, 2021 at 6:36am
closed account (1Cj3ko23)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <bits/stdc++.h>
using namespace std;

int main(){
	
	cout << "This is Kolton's program to count and add up all the even number you type in. ";
	cout << "I'll keep track of all the even, non-negative numbers entered.";
	cout << "When your done, just enter 0 or a negative number to terminate the program.\n";
	
	int number;
	int sum;
	int count = 0;
	while(number > 0){
		
		cout << "Next number? \n";
		cin >> number;
		
		if(number % 2 == 0){
			count = count + 1;
		}
		
	}
	
	cout << count << " even numbers were entered for a total sum of " << sum << endl;
	cout << "Thanks for playing!! Goodbye.\n";
	
	return 0;
}


This is what I have so far and I can't get the code to let me input a number. Also, don't know to to get it to add only the even numbers.
Last edited on Feb 12, 2021 at 7:10am
Feb 12, 2021 at 7:05am
Line 10, 11: number and sum are uninitialized (garbage).

Line 13: First time through the while loop, you're testing against garbage. If number is negative or zero, you will never execute your loop.

Line 19: You're counting even numbers correctly, You just need to add:
sum += number;

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

Feb 12, 2021 at 7:13am
closed account (1Cj3ko23)
Why would I get rid of number and sum on lines 10, 11? Also, how do I implement the sum += number; ?
Feb 12, 2021 at 7:24am
I didn't tell you to get rid of number and sum. I told you they need to be initialized.
10
11
number = 1;  // Must be > 0 or will never enter the while loop the first time
sum = 0;



how do I implement the sum += number;

Exactly the way I showed you. Or:
sum = sum + number;
Feb 12, 2021 at 10:28am
Consider:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
using namespace std;

int main() {
	cout << "This is Kolton's program to count and add up all the even number you type in.\n";
	cout << "I'll keep track of all the even, non-negative numbers entered.\n";
	cout << "When your done, just enter 0 or a negative number to terminate the program.\n";

	int sum {};
	int count {};

	for (int number {}; (cout << "Next Number: ") && (cin >> number) && (number > 0); )
		if (number % 2 == 0) {
			++count;
			sum += number;
		}

	cout << count << " even numbers were entered for a total sum of " << sum;
	cout << "\nThanks for playing!! Goodbye.\n";

	return 0;
}



This is Kolton's program to count and add up all the even number you type in.
I'll keep track of all the even, non-negative numbers entered.
When your done, just enter 0 or a negative number to terminate the program.
Next Number: 17
Next Number: 72
Next Number: 33
Next Number: 48
Next Number: 21
Next Number: 0
2 even numbers were entered for a total sum of 120
Thanks for playing!! Goodbye.

Feb 13, 2021 at 12:34am
1
2
3
4
int number;
	int sum;
	int count = 0;
	while(number > 0){


this is why the do-while exists. initialize variables is also important, but the condition after the loop is very handy as well and here, it would work even if number remains uninitialized
do
{
cin >> number;
stuff;
}
while (number >0)
Topic archived. No new replies allowed.