Need help with boolean.

I'm not sure what I am doing wrong, can someone please help me.

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
 // Program EvenOrOdd.cpp demonstrates common uses of SVRF
//         functions with value parameters.
//		   Function IsEven is called. It is passed a single 
//		   integer parameter. If this value is even, the
//		   function returns true else false is returned.

#include <iostream>		// For cin / cout
#include <iomanip>		// For setw
using namespace std;

// Funtion prototype
void IsEven(int num);						// True if a number is even else false

int main ()
{  
	int num;	// Value to be tested

	cout << "Please enter a number. "<<endl;
	cin >> num;

	// Fill in the function call to IsEven
	IsEven(num);
	if(numIsEven = true)
		cout << num << " is an even number. " << endl;
	else
		cout << num << " is an odd number. " << endl;

    return 0;
}

// Fill in the function definition:
void IsEven(int num)

// Fill in the function body:
{
	/*
		Purpose: IsEven is passed a single integer and determines
		if it is even or odd.

		Pre: The parameter is a single integer.

		Post: The function returns 
	*/

	// Even or odd code belongs here
	bool numIsEven = false;

}
first of all lon line 21 you are using the wrong operator it should be == for comparison. Secondly numIsEven is only defined in the Iseven function. Thirdly you should return a boolean on your iseven function.


Here's 2 ways to check:

1
2
3
4
5
6
7
8
9
10
11
bool isEven( int num )
{
    return( !(num & 1) ); //if last bit is a 1 return 0 , if last bit is 0 return 1
    //or ---   return( num & 1 == 0 );
}

bool isEven( int num )
{
    return( !(num % 2) ); //if it can be evenly divided by 2 return 1 , otherwise return 0
    //or --- return( num % 2 == 0 );
}



*By the way you are supposed to fill in the spaces your teacher provided not copy and paste the function hoping it is already filled out.
Last edited on
Thank you for your help, but I'm still not understanding it. Here is what I have now though:
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
// Program EvenOrOdd.cpp demonstrates common uses of SVRF
//         functions with value parameters.
//		   Function IsEven is called. It is passed a single 
//		   integer parameter. If this value is even, the
//		   function returns true else false is returned.

#include <iostream>		// For cin / cout
#include <iomanip>		// For setw
using namespace std;

// Funtion prototype
void IsEven(int num);						// True if a number is even else false

int main ()
{  
	int num;	// Value to be tested

	cout << "Please enter a number. "<<endl;
	cin >> num;

	// Fill in the function call to IsEven
	IsEven(num);
	if(IsEven(num) == true)
		cout << num << " is an even number. " << endl;
	else
		cout << num << " is an odd number. " << endl;

    return 0;
}

// Fill in the function definition:
void IsEven(int num)

// Fill in the function body:
{
	/*
		Purpose: IsEven is passed a single integer and determines
		if it is even or odd.

		Pre: The parameter is a single integer.

		Post: The function returns 
	*/

	// Even or odd code belongs here
	bool numIsEven (int num)
{
	if ( !(num % 2== 0 )
     return true;
  else 
     return false;
     //if it can be evenly divided by 2 return 1 , otherwise return 0
    //or --- return( num % 2 == 0 );
}

}
Last edited on
you weren't supposed to copy my function and put it inside of yours. Also you like combined the two of the separate examples provided into some weird version anyways you are missing a parenthesis. Now I am getting the understanding it is not the boolean that is confusing you but how to call/use functions. Check out these links:
http://www.cplusplus.com/doc/tutorial/functions/
http://www.cplusplus.com/doc/tutorial/functions2/
http://www.learncpp.com/ <--------chapter 7


Basically it's like this

1
2
3
4
5
6
7
8
9
10
[return type] [function name][(parameters)]
{
    //do stuff here
    //return what ever needs to be returned
}

//When you call it now the function is equal (==) to the return value.
//Say it returns true
//Then (isEven() == true) == true



*On a side note
There are other ways to check if a number is even or not I was just showing two ways that you could do it.
Last edited on
I just don't understand if I need both the void function and the bool underneath it. This is driving me insane trying to figure this out. My teacher said there is no return for void functions, so I'm confused.

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
IsEven(num);
	if(IsEven() == true) == true
		cout << num << " is an even number. " << endl;
	else
		cout << num << " is an odd number. " << endl;

    return 0;
}

// Fill in the function definition:
void IsEven(int num)

// Fill in the function body:
{
	/*
		Purpose: IsEven is passed a single integer and determines
		if it is even or odd.

		Pre: The parameter is a single integer.

		Post: The function returns 
	*/

	// Even or odd code belongs here
	bool numIsEven (int num)
{
	if ( !(num % 2== 0 )
     return true;
  else 
     return false;

	return bool;
}

}


}
You need to write a function named isEven().
This function should return a bool, so you know its return type is bool.
This function should also take one argument - an integer.
The function should return true if the argument number can be evenly divided by two, other wise, it should return false.

in your main() function, you could call it like this:

1
2
3
4
5
6
7
8
9
10
int main() {
	int number = 2;
	if(isEven(number)==true) {
		std::cout << "The number is even" << std::endl;
	}else {
		std::cout << "The number is odd" << std::endl;
	}
	std::cin.get();
	return 0;
}
Last edited on
I just changed it from a void function to a normal SVRF and that seemed to fix all of my problems. It says I'm missing a parenthesis or something though. Sorry if I'm wasting your time with 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
 // Funtion prototype
int IsEven(int num);						// True if a number is even else false

int main ()
{  
	int num;	// Value to be tested

	cout << "Please enter a number. "<<endl;
	cin >> num;

	// Fill in the function call to IsEven
	if(IsEven(num) == true) {
		cout << num << " is an even number. " << endl;
	}else{
		cout << num << " is an odd number. " << endl;
	{
    return 0;
}

// Fill in the function definition:
int IsEven(int num)
{
// Fill in the function body:

	/*
		Purpose: IsEven is passed a single integer and determines
		if it is even or odd.

		Pre: The parameter is a single integer.

		Post: The function returns 
	*/

	// Even or odd code belongs here
	if (num / 2== 0 )
     return true;
  else 
     return false;

	return bool;
}

}
SVRF? It seems to me you have only been taught ints and not booleans.
A boolean is false if it is equal to 0 and it is true if it is anything else. Kind of odd having it with a return type of int and then returning booleans. It will work but looks weird. If you are returning ints might as well return 0/1. Also your logic for even/odd is wrong. The only way for yours to be even is if the num is 0. It should be % (modulus) not / (divide) on line 35. Functions can return 99% things in c++ instead of just void (nothing) and int(integers).
Topic archived. No new replies allowed.