954,505 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

help writting a c++ programme

i have written this program.
it excecutes without any errors and even gives me the correct answer.
the program is to tell whether the integer is odd or even.
the problem is that i want a series of integers and passes them one at a time to function even.

Sample Screen display
Enter an integer: 8
8 is an even integer
Enter an integer: 3
3 is an odd integer
Enter an integer: 99
99 is an odd integer


This is the programme i have written so far, i just want to know what im doing wrong and how to correct it.

include <iostream>

bool even(int x);

using namespace std;

void main()

{
int x;

std::cout << "Enter an integer: ";

std::cin >> x;


if(even(x))

std::cout << x << " is an even number." << endl;
else

std::cout << x << " is an odd number." << endl;

}

bool even(int x1)

{

return (x1%2 == 0);

}
johny112
Newbie Poster
12 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

Use code tags :D

the problem is that i want a series of integers and passes them one at a time to function even.

If you mean that you want to implement the same algorithm for a series of numbers, use arrays. If you want to pass them one at a time to the function, then put the function call inside a loop.

Jishnu
Posting Pro
518 posts since Oct 2006
Reputation Points: 193
Solved Threads: 25
 

thanks for reply and i got the programme to work.

Now i have to modify the programme, but the outcome should be same. "The function should take an integer argument and return true if the integer is even and false otherwise."

Now i think i have to use bool statement, but cant exactly figure it out.
thanks for reply in advance.

johny112
Newbie Poster
12 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

So what have you figured out?

Didn't you read the Rules and Read Me: Read This Before Posting yet?

WaltP
Posting Sage w/ dash of thyme
Moderator
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

what i have figured out is the bool statements in the program i have written in my previous posts, but not sure how to correct it.

johny112
Newbie Poster
12 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

What? there is nothing to correct because the function even already returns true or false, unless you just want to rewrite it like this:

bool even(int x1)

{

return (x1%2 == 0) ? true : false;

}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

i you think thats ok, that thats fine thanks again for your replies.

The reason i said so was because even if i delete all bool statements, the program still executes perfectly, with same outcome.

But anyways if it doesnt make a difference, then i would leave it as it is.

johny112
Newbie Poster
12 posts since Nov 2007
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You