944,157 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4522
  • C++ RSS
Nov 27th, 2007
0

help writting a c++ programme

Expand Post »
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.


c++ Syntax (Toggle Plain Text)
  1. include <iostream>
  2.  
  3. bool even(int x);
  4.  
  5. using namespace std;
  6.  
  7. void main()
  8.  
  9. {
  10. int x;
  11.  
  12. std::cout << "Enter an integer: ";
  13.  
  14. std::cin >> x;
  15.  
  16.  
  17. if(even(x))
  18.  
  19. std::cout << x << " is an even number." << endl;
  20. else
  21.  
  22. std::cout << x << " is an odd number." << endl;
  23.  
  24. }
  25.  
  26. bool even(int x1)
  27.  
  28. {
  29.  
  30. return (x1%2 == 0);
  31.  
  32. }
Last edited by Ancient Dragon; Nov 27th, 2007 at 6:31 pm. Reason: add code tags
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johny112 is offline Offline
12 posts
since Nov 2007
Nov 27th, 2007
0

Re: help writting a c++ programme

Use code tags

Quote ...
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.
Reputation Points: 193
Solved Threads: 25
Posting Pro
Jishnu is offline Offline
518 posts
since Oct 2006
Nov 27th, 2007
0

Re: help writting a c++ programme

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johny112 is offline Offline
12 posts
since Nov 2007
Nov 27th, 2007
0

Re: help writting a c++ programme

So what have you figured out?

Didn't you read the Rules and Read Me: Read This Before Posting yet?
Moderator
Reputation Points: 3281
Solved Threads: 896
Posting Sage
WaltP is offline Offline
7,749 posts
since May 2006
Nov 27th, 2007
0

Re: help writting a c++ programme

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johny112 is offline Offline
12 posts
since Nov 2007
Nov 27th, 2007
0

Re: help writting a c++ programme

What? there is nothing to correct because the function even already returns true or false, unless you just want to rewrite it like this:
C++ Syntax (Toggle Plain Text)
  1. bool even(int x1)
  2.  
  3. {
  4.  
  5. return (x1%2 == 0) ? true : false;
  6.  
  7. }
Last edited by Ancient Dragon; Nov 27th, 2007 at 6:41 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Nov 28th, 2007
0

Re: help writting a c++ programme

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johny112 is offline Offline
12 posts
since Nov 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Alternatives to arrays?
Next Thread in C++ Forum Timeline: Managed and unmanaged code





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC