Even Number?

Hi, I'm doing the even number checking, but somehow the code does't work, can I know which part I did wrong? TQ...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <conio>

void is_even(int a); //Function prototype

int main(void)
{
	int a;
   cout<<"Enter an integer:\n";
   cin>>a;
   cout<<"Is "<<a<<" even? ";
   is_even (a);
   cout<<endl;
   getch();
   return 0;
}

is_even(int a)
{
	if(a/2==0)
   {
   	cout<<"Yes";
   }
}
1
2
3
4
5
6
7
8
void is_even(int a)
{
    //if(a/2==0)
    if( a%2 == 0 )
   {
   	cout<<"Yes";
   }
}


EDIT: > You forget to specify return value void in your function definition: corrected, thanks.
Last edited on
You forget to specify return value void in your function definition. Also your function does not display anyting if a number is odd.
Thanks...I've modified it to 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
#include <iostream>
#include <conio>

void is_even(int a); //Function prototype

int main(void)
{
	int a;
   cout<<"Enter an integer:\n";
   cin>>a;
   cout<<"Is "<<a<<" even? ";
   is_even (a);
   cout<<endl;
   getch();
   return 0;
}

void is_even(int a)
{
	if(a%2==0)
   {
   	cout<<"Yes";
   }
   else
   {
   	cout<<"No";
   }
}
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
#include <iostream>
#include <conio.h> // Not <conio>
using namespace std; // You forgot this
void is_even(int a); //Function prototype

int main(void)
{
	int a;
   cout<<"Enter an integer:\n";
   cin>>a;
   cout<<"Is "<<a<<" even? ";
   is_even (a);
   cout<<endl;
   getch();
   return 0;
}

void is_even(int a) // You needed to make this a void function
{
	if(a%2==0) // should be %, not /
   {
   	cout<<"Yes";
   }
	else	//What about the other case?
		cout << "No"; // What about the other case?
}
Last edited on
In general it is a bad idea to forse a function to perform two or more tasks. In case of your function it performs two task: determinates whether a number is even and sends some data in output stream. It would be better to separate these tasks.
For example you could define your function as

1
2
3
4
inline bool  is_even( int x )
{
   return ( x % 2 == 0 );
}


and in main you could write


1
2
   cout<<"Is "<<a<<" even? ";
   cout << ( is_even ( a ) ? "yes" : "no" ) << std::endl;
Last edited on
In general it is a bad idea to forse a function to perform two or more tasks.


Thanks..Is that means function are not allow to perform more then 1 task? May I know the reason? TQ...
The more that you make a function do, the less it is useful for.

What vlad is saying is that it would be better for is_even() to just perform the test, not cout anything.

You can make your code a little nicer by doing this instead.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
using namespace std;
bool is_even(int a);

int main(void)
{
	int a;
	cout<<"Enter an integer:";
	cin>>a;
	if (is_even(a))
		cout << a << " is even" << endl;
	else
		cout << a << " is not even" << endl;
	return 0;
}

bool is_even(int a)
{
	if(a%2)		return false;
	return true;
}


That can be reduced further to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;
bool is_even(int a);

int main(void)
{
	int a;
	cout<<"Enter an integer:";
	cin>>a;
	cout<< a << " is " << (is_even(a) ? "" : "not") << " even" << endl;
	return 0;
}

bool is_even(int a) // You needed to make this a void function
{
	if(a%2)		return false;
	return true;
}
Enter an integer:3
3 is not even
Press any key to continue . . .
Last edited on
No I did not say that function are not allowed to perform more than one task. I said that it is better when you code consists from simple fucntions with clear logic.

To demonstrate that in case of the original example it is better that function is_even perform only one task consider situation that you need not to display string literals "yes" or "no" but you need to determine whether a value is even or odd in conditional statement. For example, assume that you need to calculate the sum of even values of some array. In this case you could write very simple code


1
2
3
4
5
6
7
8
9
10
11
int a[] = { 1, 5, 4, 7, 8, 5, 4, 9, 2, 5 };

int odd_sum = 0, even_sum = 0;
for ( int i = 0; i < sizeof( a ) / sizeof( *a ); i++ )
{
   if ( is_even( a[i] ) ) even_sum += a[i];
   else odd_sum += a[i];
}

std::cout << "sum of even values is " << even_sum << std::endl;
std::cout << "sum of odd values is " << odd_sum << std::endl;



So if your function perform only one task it becomes more universal and its logic is very clear for its uses.
Last edited on
@vlad

I can't run ur code...don't know what it means by "cannot convert void to bool" in the error...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <conio>

void is_even(int a);

int main(void)
{
int a[] = { 1, 5, 4, 7, 8, 5, 4, 9, 2, 5 };

int odd_sum = 0, even_sum = 0;
for ( int i = 0; i < sizeof( a ) / sizeof( *a ); i++ )
{
   if ( is_even( a[i] ) ) even_sum += a[i];
   else odd_sum += a[i];
}

std::cout << "sum of even values is " << even_sum << std::endl;
std::cout << "sum of odd values is " << odd_sum << std::endl;

getch ();
return 0;
}


Can u help correct the code? Because I wanted to know how actually it's doing...Thanks...

Change line 4 to:
bool is_even(int a);
atjm88,
you are wrong. It is not my code that you cannot compile. It is your code that you can not compile. Please look one more what code I suggested.
Thanks..Is that means function are not allow to perform more then 1 task? May I know the reason? TQ...


It's called cohesion. To perform one task and perform it well. It has a number of benefits such as the function is more general (more reusable), easier to test, contains less logic (easier to understand), etc..

http://en.wikipedia.org/wiki/Cohesion_%28computer_science%29

As developers we always pursue high cohesion and loose coupling.
Thanks...
For vlad, I tried to run in VSC++ 2010 but still cannot...

This is the error...

1>c:\users\128a5s5\documents\visual studio 2010\projects\22\22\22.cpp(20): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
1>          c:\program files\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
1>22.obj : error LNK2019: unresolved external symbol "bool __cdecl is_even(int)" (?is_even@@YA_NH@Z) referenced in function _main
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
1>c:\users\128a5s5\documents\visual studio 2010\Projects\22\Debug\22.exe : fatal error LNK1120: 2 unresolved externals
Last edited on
The linker does not see definition of is_bool. Did you include defeinition (function body) of the function?
Last edited on
This is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <conio.h>

bool is_even(int a);

int main(void)
{
int a[] = { 1, 5, 4, 7, 8, 5, 4, 9, 2, 5 };

int odd_sum = 0, even_sum = 0;
for ( int i = 0; i < sizeof( a ) / sizeof( *a ); i++ )
{
   if ( is_even( a[i] ) ) even_sum += a[i];
   else odd_sum += a[i];
}

std::cout << "sum of even values is " << even_sum << std::endl;
std::cout << "sum of odd values is " << odd_sum << std::endl;

getch ();
return 0;
}



Error

1>22.obj : error LNK2019: unresolved external symbol "bool __cdecl is_even(int)" (?is_even@@YA_NH@Z) referenced in function _main
1>MSVCRTD.lib(crtexew.obj) : error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
1>C:\Users\128A5S5\documents\visual studio 2010\Projects\22\Debug\22.exe : fatal error LNK1120: 2 unresolved externals
Again is there the definition of is_bool in your code?!
Last edited on
is_bool?? May I know where u see is_bool? I only use is_even...thanks...
I made a typo. But as for you you should switch on your brain. I am not going to repeat the same several times.
Last edited on
Topic archived. No new replies allowed.