Hi. I am writing a program to read user input numbers until '0' is entered. I then figure out if each number is even or odd and find the average of all the evens and all the odds (even average is one number, odd average is another.) I have it running where I count the evens and odds, but don't know how to add and average the two groups. Can someone explain how to do this? Thanks so much in advance. Here is my code so far.

#include <iostream>
using namespace std;
int even=0;
int odd=0;
int number=0;
int main()
{
	 
	int i=0; //holds the index for our loop.    
	int input=0;    
	//int lowest = 0; //holds the lowest number.    
	//int highest = 0; //holds the highest number.    
	int eventotal = 0; 
	int oddtotal = 0;
		    
do
{
	cout << "Please enter a number: "<< endl;        
	cin >> number;   
	if ( (number%2 == 0) && (number > 0) )  
		even++;
	else if (number > 0) 
		odd++;
}
while (number!=0);	
	
	cout << "You have entered" << ' '<< odd << ' ' <<"odd numbers." << endl;
	cout << "And" << ' ' << even << ' ' <<"even numbers." << endl;	

	/*cout << "Average: " << total/input << "." << endl;    
	cout << "lowest: " << lowest << " highest: " << highest << "." << endl;   */ 
	
	return 0;}

hi
you have to add this to your code;

// replace your loop with this one
do
{
	cout << "Please enter a number: "<< endl;        
	cin >> number;   
	if ( (number%2 == 0) && (number > 0) )  
		{
                   even++;
                   eventotal = eventotal + number;
}
	else if (number > 0) 
		{odd++;
                 oddtotal = oddtotal +number;}
}
while (number!=0);
int oddavg ;// to store the average of odd numbers  
oddavg = oddtotal/ odd;
int evenavg; // to store the average of even numbers
evenavg = eventotal / even;

That works great. Except it crashes if no odd or no even value has been entered. Any ideas how to fix that?

ok replace line 17-20 in the code i wrote with this

int evenavg;
int oddavg 
if (odd==00)
cout <<" There's no odd numbers to find there average "<<endl;
else oddavg = oddtotal/ odd;
if (even == 0)
cout<" There's no even numbers to find there average "<<endl;
else evenavg = eventotal / even;

please if i helped you mark as solved

Thanks...I did this instead and it worked:

int oddavg = 0;// to store the average of odd numbers  
if(oddtotal!=0)
	oddavg = oddtotal/ odd;

int evenavg = 0; // to store the average of even numbers
if(eventotal!=0)
	evenavg = eventotal / even;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.