help writting a c++ programme

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Nov 2007
Posts: 12
Reputation: johny112 is an unknown quantity at this point 
Solved Threads: 0
johny112 johny112 is offline Offline
Newbie Poster

help writting a c++ programme

 
0
  #1
Nov 27th, 2007
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.


  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
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Solved Threads: 26
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: help writting a c++ programme

 
0
  #2
Nov 27th, 2007
Use code tags

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.
"You know you're a computer geek when you try to shoo a fly away from the monitor screen with your cursor. That just happened to me. It was scary." - Juuso Heimonen.

"The only truly secure computer is one buried in concrete, with the power turned off and the network cable cut." - Anonymous.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 12
Reputation: johny112 is an unknown quantity at this point 
Solved Threads: 0
johny112 johny112 is offline Offline
Newbie Poster

Re: help writting a c++ programme

 
0
  #3
Nov 27th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,117
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 282
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: help writting a c++ programme

 
0
  #4
Nov 27th, 2007
So what have you figured out?

Didn't you read the Rules and Read Me: Read This Before Posting yet?
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 12
Reputation: johny112 is an unknown quantity at this point 
Solved Threads: 0
johny112 johny112 is offline Offline
Newbie Poster

Re: help writting a c++ programme

 
0
  #5
Nov 27th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,508
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: help writting a c++ programme

 
0
  #6
Nov 27th, 2007
What? there is nothing to correct because the function even already returns true or false, unless you just want to rewrite it like this:
  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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 12
Reputation: johny112 is an unknown quantity at this point 
Solved Threads: 0
johny112 johny112 is offline Offline
Newbie Poster

Re: help writting a c++ programme

 
0
  #7
Nov 28th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC