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.
#include <bits/stdc++.h>
usingnamespace 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.
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.
#include <iostream>
usingnamespace 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.
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)