User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 455,975 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,758 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser: Programming Forums
Views: 2021 | Replies: 6 | Solved
Reply
Join Date: Nov 2007
Posts: 12
Reputation: johny112 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
johny112 johny112 is offline Offline
Newbie Poster

help writting a c++ programme

  #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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2006
Location: India
Posts: 514
Reputation: Jishnu will become famous soon enough Jishnu will become famous soon enough 
Rep Power: 5
Solved Threads: 25
Jishnu's Avatar
Jishnu Jishnu is offline Offline
Posting Pro

Re: help writting a c++ programme

  #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  
Join Date: Nov 2007
Posts: 12
Reputation: johny112 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
johny112 johny112 is offline Offline
Newbie Poster

Re: help writting a c++ programme

  #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  
Join Date: May 2006
Posts: 2,779
Reputation: WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold WaltP is a splendid one to behold 
Rep Power: 15
Solved Threads: 229
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Maven

Re: help writting a c++ programme

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

Didn't you read the Rules and Read Me: Read This Before Posting yet?
Age is unimportant -- except in cheese
Reply With Quote  
Join Date: Nov 2007
Posts: 12
Reputation: johny112 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
johny112 johny112 is offline Offline
Newbie Poster

Re: help writting a c++ programme

  #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  
Join Date: Aug 2005
Location: near St Louis, Missouri, USA
Posts: 11,539
Reputation: Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of Ancient Dragon has much to be proud of 
Rep Power: 40
Solved Threads: 972
Moderator
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Most Valuable Poster

Re: help writting a c++ programme

  #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:
bool even(int x1)

{

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

}
Last edited by Ancient Dragon : Nov 27th, 2007 at 6:41 pm.
<<Freelance Programmer>> << Hobby Site>>
Signature links for sale. PM me for details
Reply With Quote  
Join Date: Nov 2007
Posts: 12
Reputation: johny112 is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
johny112 johny112 is offline Offline
Newbie Poster

Re: help writting a c++ programme

  #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  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb C++ Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 9:19 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC