Program that counts seperately the total of odd and the total of even numbers

I am trying to write a program that stores the seperately the tota of 10 inputted numbers depending on whether they are odd or even, and also add counters for how many odd or even numbers there are. For example, if the inputted numbers are 1.2.3.4.5.6.7.8.9.10 tit shoulde be 5 odd and 5 even numbers, the total of odds should be 25 and the total of evens should be 30.
The problem is, I can't understand how to seperate my odds and evens to different counters with my booleans. It always counts everything just as odds, or as even numbers.

#include <iostream>

#define N 10

using namespace std;


void outputResults(int numOfOdd, int oddTotal, int numOfEven, int evenTotal) ;

bool numIsOdd(int myNum);
bool isEven(int myNum);

int oddTotal;
int evenTotal;



int numOfEven;
int numOfOdd;

int myNum[N];


int myNumOdd;
int myNumEven;

int main() {

cout << " Please input 10 numbers " << endl;



for (int i = 0; i < N; i++) {
std::cin >> myNum[i];
oddTotal = myNum[i] - 1;
numOfOdd = oddTotal + 1;
oddTotal = numOfOdd ;
numOfEven = myNum[i] + numOfEven;

}


for (int i = 0; i < N; i++) {
numIsOdd(myNum[i]);
isEven(myNum[i]);
}
outputResults(numOfOdd, oddTotal, numOfEven, evenTotal);



cin.get();
return 0;
}

bool isOdd(int myNum){

return myNum % 2 == 0;
}

bool isEven(int myNum) {

return myNum % 1 == 0;
}


void outputResults(int numofOdd, int oddTotal, int numOfEven, int evenTotal) {


for (int i = 0; i < N; i++) {
if (isOdd) {
myNumOdd = myNum[N];
for (int i = 0; i < N; i++) {
for (int n = 0; n < i; n++) {
oddTotal = (myNum[n] + myNumOdd);
}
}
}
}


for (int i = 0; i < N; i++) {
if (isEven) {
myNumEven = myNum[N];
for (int i = 0; i < N; i++) {
for (int t = 0; t < i; t++) {
evenTotal = (myNum[t] + myNumEven);
}
}
}
}
cout << " Number of Odds is " << numofOdd << " numer of even is " << numOfEven << endl;
cout << " Even Total is :" << evenTotal << " Odd total is " << oddTotal << endl;


}
Last edited on
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/

HINT: you can edit your post and add code tags.

Look at the logic for determining odds or evens.

In isEven(), doing a modulo with 1 will ALWAYS leave a result of zero with positive integers. So every number, odd or even, is treated as an even number.

In isOdd(), doing a modulo with 2 with a zero result is the proper way to check if a number is even, not odd.

Odd numbers are num % 2 != 0
You don't need to record the numbers themselves. Just update the statistics as you read the numbers.

You don't need isOdd() and isEven(). Just one will do. If it isn't even then it's odd.

Here's a fragment of your code after it's been fixed:

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
29
30
31
32
33
int
main()
{

    cout << " Please input 10 numbers " << endl;

    for (int i = 0; i < N; i++) {
        std::cin >> myNum;
        // If the number is even then update evenTotal and numOfEven
        // else update oddTotal and numOfOdd
    }

    // Print out the results
    outputResults(numOfOdd, oddTotal, numOfEven, evenTotal);

    cin.get();
    return 0;
}

bool
isEven(int myNum)
{
    return myNum % 2 == 0;
}


void
outputResults(int numofOdd, int oddTotal, int numOfEven, int evenTotal)
{
    cout << " Number of Odds is " << numofOdd << " numer of even is " << numOfEven <<
        endl;
    cout << " Even Total is :" << evenTotal << " Odd total is " << oddTotal << endl;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cmath>
bool is_even(int n) { return std::abs(n) % 2 == 0; }

int main()
{
  int n_evens = 0, n_odds = 0, sum_evens = 0, sum_odds = 0;
  for (int n; std::cin >> n; ) 
  {
    (is_even(n)? n_evens : n_odds)++;
    (is_even(n)? sum_evens : sum_odds) += n;
  }

  std::cout << n_evens << " even numbers with a sum of " << sum_evens << '\n';
  std::cout << n_odds << " odd numbers with a sum of " << sum_odds << '\n';
}
Last edited on
Even more simplification possible,

number_of_odds = 10 - no_of_evens ( or N - no_of_evens )

OP's effort, as strenuous as it appears, would have benefited from some sort of pseudocode plan before punching out code.


setup odds and evens piles and other counters, blah blah

:loop 10 times
    get a number

    if number is odd 
        put number in the 'next available spot' on the odds pile
        keep count of odds
        
       otherwise put number -ditto- on the evens pile
loop back:

display results


EDIT: On reviewing OP numbers don't have to be put into separate 'piles' (read containers of some sort, only the odd/even total's etc, and count of at least one type)

Last edited on
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
#include <iostream>
#include <string>
using namespace std;

class NumType
{
   int count = 0;
   int sum = 0;
public:
   void inc( int n ) { count++; sum += n; }
   void write( string title = "" ) { cout << title << "   count: " << count << "   sum: " << sum << '\n'; }
};

int main()
{
   const int N = 10;
   NumType odd, even;
   cout << "Enter " << N << " numbers: ";
   for ( int i = 0; i < N; i++ )
   {
      int n;
      cin >> n;
      n % 2 ? odd.inc( n ) : even.inc( n );
   }
   odd.write ( "Odd: " );
   even.write( "Even:" );
}

Enter 10 numbers: 1 2 3 4 5 6 7 8 9 10
Odd:    count: 5   sum: 25
Even:   count: 5   sum: 30
Last edited on
Topic archived. No new replies allowed.