// 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
usingnamespace 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.
// 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
usingnamespace 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 )
returntrue;
elsereturnfalse;
//if it can be evenly divided by 2 return 1 , otherwise return 0
//or --- return( num % 2 == 0 );
}
}
[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.
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.
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 )
returntrue;
elsereturnfalse;
returnbool;
}
}
}
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;
}
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.
// 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 )
returntrue;
elsereturnfalse;
returnbool;
}
}
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).