AVGTIL0 – Write a program to accept integers from the keyboard until a zero is entered. Print the sum and average of the even numbers. Ignore any odd numbers in your calculations and print an appropriate message. I am working on this program more than two days.

    #include "stdafx.h"
    #include <iostream>
    #include <iomanip>
    #include <string>
    #include <fstream>
    #include <stdio.h>

    using namespace std;

    void Pause() 
    { 
      string junk;
      cout << "Press any to continue ... "; 
      cin.ignore();
      getline(cin, junk);
    }

int main()
{
        // declare file variables
        // int x, evenNumber, averageEven;
           int entNum, ans;
           int even, x;
           int odd;

        // get input
           cout  << "What is the integer of X: ";
           cin >> x;
           cout << endl;

        //display results

          do
        {
          even=0;
          odd=0;
          scanf("%d",& entNum);
          ans = entNum%2;

          if(ans==0)
          {
            even++;
          }
          else if(ans!=0) 
            {
            odd++;
            }   
        }
          while(entNum!=0);

          // if (x %2==0)

          // else if ( x==0)
          // averageEven = evenNumber
          cout <<" average of the even numbers is: "<< even << endl;

        // freeze screen
           Pause();
           return (0);
}

When I run the program give me the same result whatever I change on it. I need help
Thanks,

Here is a simple solution that I came up with. Let me know if that helps or if you have any questions about it.

#include <iostream>

using namespace std;

bool isOdd(int input);

int main()
{
    int input;
    int sum = 0;
    int count = 0;

    do
    {
        cout << "Enter a number: ";
        cin >> input;

        if(isOdd(input) == true && input != 0)
        {
            sum = sum + input;
            count++;
        }

    }while(input != 0)

    cout << "The total is: " << sum << endl;
    cout << "The average is: " << sum / count << endl;

  return 0;

}

bool isOdd(int input)
{
    if(input % 2== 0)
    {
        return true;
    }
    else
        return false;
}

You also needed to consider that 0 is also an even number, which is why I added the statement "&& input != 0)"

Edited 1 Week Ago by Andrew_36

and change sum to oddSum and count to oddCount

and add an else if (inOdd(input) == false && input != 0 to the if statement with the same code as the first part of the if statement except evenSum and evenCount