please help me it doesnt work , i dont know whats wrong

4. Write a program that reads positive integers until a –1 is entered. Once input terminates, the program should report the total number of even numbers entered, the average value of the even numbers and the same for odd numbers. The program should also indicate which of the two categories (odd or even) has the highest count and the highest average.

Sample Run:
Please input the numbers, -1 to stop:
3
6
12
4
13
12
8
2
133
-1
There were 6 even numbers and their average was 7.33
There were 3 odd numbers and their average was 49.66
There were more even numbers than odd numbers
Odd numbers average is higher









and my code is:





#include <iostream>

using namespace std;


#include <iomanip>

using std::setprecision;

int main()

{

int even = 0;
int odd = 0;
int x;
int sum_e =0 , sum_o =0;
double avg1=0;
double avg2=0;

cout << "please input the numbers ,-1 to stop\n";
cin >> x;

while (x!=-1);
{

if (x%2==0)

x=even;
++even;
sum_e = sum_e + x;

else if (x%2!=0)
{
x= odd;
++odd;
sum_o = sum_o + x;
};

cout << "please input the numbers ,-1 to stop\n";
cin >> x;


};


if ( even!=0 && odd!=0)
{

avg1 = static_cast< double >(sum_e)/even;
avg2 = static_cast< double>(sum_o)/ odd;

cout << "there were "<< even <<"numbers and their average was" << (2) << fixed << avg1 << endl;
cout << "there were" << odd << "numbers and their average was "<< setprecision(2) << fixed << avg2 << endl;

};


if (even > odd)

cout << " there were more even numbers than odd numbers\n ";
cout << " odd number average is higher" << endl;

if (odd > even)

cout << "there were more odd numbers than even numbers\n ";
cout << "even number average is higher" << endl;


return 0;

}
Last edited on
please if anyone knows whats wrong..

please help asap:`(
please if anyone knows whats wrong..

Maybe if you described what makes you think it isn't working, someone would have a better place to start than "It doesn't work."

Use code tags. http://www.cplusplus.com/articles/jEywvCM9/
Read: http://www.cplusplus.com/forum/beginner/1/
First the errors in your code I found...

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
using namespace std;

#include <iomanip>	// you do not need it.
using std::setprecision; // you do not need it.

int main()
{
int even = 0;
int odd = 0;
int x;
int sum_e =0 , sum_o =0;
double avg1=0;
double avg2=0;

cout << "please input the numbers ,-1 to stop\n";
cin >> x;

while (x!=-1);	// I would use a do {} while (); loop here so it does not go through the ifs for -1.
{

if (x%2==0)
					// You miss '{'
x=even;				// This might be nice as a comment, but otherwhise I see no use for it here...
++even;				// Im used to "even++;", but if it works like this, okay...
sum_e = sum_e + x;
					// You miss '}'
else if (x%2!=0)
{
x= odd;				// This might be nice as a comment, but otherwhise I see no use for it here...
++odd;				// Im used to "odd++;", but if it works like this, okay...
sum_o = sum_o + x;
};					// Do not use ';' after '}' when using if blocks.

cout << "please input the numbers, -1 to stop\n";
cin >> x;


};		// again, no ';' after while loops.


if ( even!=0 && odd!=0)
{

avg1 = static_cast< double >(sum_e)/even;			// division through zero when even = 0!
avg2 = static_cast< double>(sum_o)/ odd;			// same as the line above

cout << "there were "<< even <<"numbers and their average was" << (2) << fixed << avg1 << endl;		// (2) alone?
cout << "there were" << odd << "numbers and their average was "<< setprecision(2) << fixed << avg2 << endl;		// if you use setprecision(2), you do not need fixed.

};		// again, no ';' after the if loop.


if (even > odd)		// without the {...} it only counter for one line... and your next is empty...

cout << " there were more even numbers than odd numbers\n ";	// guess to be a part of your if block
cout << " odd number average is higher" << endl;				// have a look above... | The average of your odd numbers can still be higher, even though they have fewer numbers.

if (odd > even)			// same problem as with the if block above							

cout << "there were more odd numbers than even numbers\n ";		// have a look above...
cout << "even number average is higher" << endl;				// have a look above...

return 0;
}



And here a solution how you could do it... (Im no expert, so if anything is bad style or something, point it out...):

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// MAIN_CPP
#include <iostream>
using namespace std;

int main()
{
	int		counter_even	= 0,
			counter_odd		= 0,
			input			= 0,
			sum_even		= 0,
			sum_odd			= 0;
	double	average_even	= 0,
			average_odd		= 0;

	cout << "Please input a number to check on. If you enter -1, the programm will stop." << endl;
	cin >> input;

	do{
		if (input % 2 == 0)
		{
			cout << "This number is even!" << endl;
			counter_even++;
			sum_even += input;
		}
		else if (input % 2 != 0)
		{
			cout << "This number is odd!" << endl;
			counter_odd++;
			sum_odd += input;
		}
		else
			cout << "There was an error during the even/odd identification!" << endl;

		cout << "Please input a number to check on. If you enter -1, the programm will stop." << endl;
		cin >> input;
	} while (input != -1);

	if (counter_even != 0)
		average_even = static_cast<double>(sum_even) / counter_even;
	else average_even = 0;
	if (counter_odd != 0)
		average_odd = static_cast<double>(sum_odd) / counter_odd;
	else
		average_odd = 0;

	cout << "There were " << counter_even << " even number(s), and their average was " << average_even << "!" << endl;
	cout << "There were " << counter_odd << " odd number(s), and their average was " << average_odd << "!" << endl;

	if (counter_even > counter_odd)
		cout << "There were more even than odd numbers." << endl;
	else if (counter_even < counter_odd)
		cout << "There were more odd than even numbers." << endl;
	else
		cout << "There was the same amount of even and odd numbers." << endl;

	if (average_even > average_odd)
		cout << "The average of the even numbers is higher than the one of the odd ones." << endl;
	else if (average_even < average_odd)
		cout << "The average of the odd numbers is higher than the one of the even ones." << endl;
	else
		cout << "The average of the even numbers is as high as of the odd ones." << endl;

return 0;
}
Last edited on
@kenrim626

thanks ... so what i was wrong about is how i was entering my if else statements ... and thank you so much...

i`ve added some details for setprecision ...to use it for my double(s)..
and it turned on this way



// MAIN_CPP

#include <iostream>

using namespace std;

#include <iomanip>

using std::setprecision;

int main()

{
int counter_even = 0,
counter_odd = 0,
input = 0,
sum_even = 0,
sum_odd = 0;

double average_even = 0,
average_odd = 0;


cout << "Please input a number to check on. If you enter -1, the programm will stop." << endl;
cin >> input;

do

{
if (input % 2 == 0)
{

cout << "This number is even!" << endl;
counter_even++;
sum_even += input;
}

else if (input % 2 != 0)
{

cout << "This number is odd!" << endl;
counter_odd++;
sum_odd += input;
}

else

cout << "There was an error during the even/odd identification!" << endl;

cout << "Please input a number to check on. If you enter -1, the programm will stop." << endl;
cin >> input;

} while (input != -1);

if (counter_even != 0)
average_even = static_cast<double>(sum_even) / counter_even;

else average_even = 0;

if (counter_odd != 0)
average_odd = static_cast<double>(sum_odd) / counter_odd;

else
average_odd = 0;

cout << "There were " << counter_even << " even number(s), and their average was " << setprecision(2) << fixed << average_even << "!" << endl;
cout << "There were " << counter_odd << " odd number(s), and their average was " << setprecision(2) << fixed << average_odd << "!" << endl;

if (counter_even > counter_odd)
cout << "There were more even numbers than odd numbers." << endl;

else if (counter_even < counter_odd)
cout << "There were more odd numbers than even numbers." << endl;

else
cout << "There was the same amount of even and odd numbers." << endl;

if (average_even > average_odd)
cout << "even numbers average is higher." << endl;

else if (average_even < average_odd)
cout << "odd numbers average is higher" << endl;

else
cout << "The average of the even numbers is as high as of the odd ones." << endl;

return 0;

}



/*
Please input a number to check on. If you enter -1, the programm will stop.
3
This number is odd!
Please input a number to check on. If you enter -1, the programm will stop.
6
This number is even!
Please input a number to check on. If you enter -1, the programm will stop.
12
This number is even!
Please input a number to check on. If you enter -1, the programm will stop.
4
This number is even!
Please input a number to check on. If you enter -1, the programm will stop.
13
This number is odd!
Please input a number to check on. If you enter -1, the programm will stop.
12
This number is even!
Please input a number to check on. If you enter -1, the programm will stop.
8
This number is even!
Please input a number to check on. If you enter -1, the programm will stop.
2
This number is even!
Please input a number to check on. If you enter -1, the programm will stop.
133
This number is odd!
Please input a number to check on. If you enter -1, the programm will stop.
-1
There were 6 even number(s), and their average was 7.33!
There were 3 odd number(s), and their average was 49.67!
There were more even numbers than odd numbers.
odd numbers average is higher
Press any key to continue
*/
@cire

thanks for info.

sorry for not being good at expressing my errors!!!



@ kenrim626


thank you so much for the help>>
i`ve noticed that my errors were in how i`ve entered my if else statemets...

and it works.. but i managed to add some details >> setprecision(2) for my average decimals and so...

it turned out like this:



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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// MAIN_CPP

#include <iostream>

using namespace std;

#include <iomanip>

using std::setprecision;

int main()

{
	int		counter_even	= 0,
			counter_odd		= 0,
			input			= 0,
			sum_even		= 0,
			sum_odd			= 0;

	double	average_even	= 0,
			average_odd		= 0;


	cout << "Please input a number to check on. If you enter -1, the programm will stop." << endl;
	cin >> input;

	do
	
	{
		if (input % 2 == 0)
		{

			cout << "This number is even!" << endl;
			counter_even++;
			sum_even += input;
		}

		else if (input % 2 != 0)
		{

			cout << "This number is odd!" << endl;
			counter_odd++;
			sum_odd += input;
		}

		else

			cout << "There was an error during the even/odd identification!" << endl;

		cout << "Please input a number to check on. If you enter -1, the programm will stop." << endl;
		cin >> input;

	} while (input != -1);

	if (counter_even != 0)
		average_even = static_cast<double>(sum_even) / counter_even;

	else average_even = 0;

	if (counter_odd != 0)
		average_odd = static_cast<double>(sum_odd) / counter_odd;

	else
		average_odd = 0;

	cout << "There were " << counter_even << " even number(s), and their average was " << setprecision(2) << fixed << average_even << "!" << endl;
	cout << "There were " << counter_odd << " odd number(s), and their average was " << setprecision(2) << fixed << average_odd << "!" << endl;

	if (counter_even > counter_odd)
		cout << "There were more even numbers than odd numbers." << endl;

	else if (counter_even < counter_odd)
		cout << "There were more odd numbers than even numbers." << endl;

	else
		cout << "There was the same amount of even and odd numbers." << endl;

	if (average_even > average_odd)
		cout << "even numbers average is higher." << endl;

	else if (average_even < average_odd)
		cout << "odd numbers average is higher" << endl;

	else
		cout << "The average of the even numbers is as high as of the odd ones." << endl;

return 0;

}



/*
Please input a number to check on. If you enter -1, the programm will stop.
3
This number is odd!
Please input a number to check on. If you enter -1, the programm will stop.
6
This number is even!
Please input a number to check on. If you enter -1, the programm will stop.
12
This number is even!
Please input a number to check on. If you enter -1, the programm will stop.
4
This number is even!
Please input a number to check on. If you enter -1, the programm will stop.
13
This number is odd!
Please input a number to check on. If you enter -1, the programm will stop.
12
This number is even!
Please input a number to check on. If you enter -1, the programm will stop.
8
This number is even!
Please input a number to check on. If you enter -1, the programm will stop.
2
This number is even!
Please input a number to check on. If you enter -1, the programm will stop.
133
This number is odd!
Please input a number to check on. If you enter -1, the programm will stop.
-1
There were 6 even number(s), and their average was 7.33!
There were 3 odd number(s), and their average was 49.67!
There were more even numbers than odd numbers.
odd numbers average is higher
Press any key to continue
*/
Last edited on
Topic archived. No new replies allowed.