Loop counting odd and even numbers

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2004
Posts: 2
Reputation: c++help is an unknown quantity at this point 
Solved Threads: 0
c++help c++help is offline Offline
Newbie Poster

Loop counting odd and even numbers

 
0
  #1
Mar 23rd, 2004
i have a program that has one little glitch but i dont know how to fix it. If you can help it would be great.

  1.  
  2. int main()
  3. {
  4. int a, b, c;
  5. b=0;
  6. c=0;
  7. do
  8. {
  9. cout << "Please enter a positive integer (negative integer to stop):"<< ' ';
  10. cin>> a;
  11. if (a%2 == 0)
  12. {
  13. c++;
  14. }
  15. else
  16. b++;
  17. }
  18. while (a>=0);
  19. cout << "You have entered" << ' '<< b << ' ' <<"odd numbers." << endl;
  20. cout << "And" << ' ' << c << ' ' <<"even numbers." << endl;
  21. system("PAUSE");
  22. return 0;
  23. }

here is the program. the problem is if i put in these numbers 3,1037,60,-43. i get a answer of 3 odd numbers and 1 even numbers. it should say 2 odd and 1 even. the program is counting the negative number as an odd where it should just end the program.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,040
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 128
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: I need some C++ help

 
0
  #2
Mar 23rd, 2004
It's a good start. Let me just tell you, the first thing I noticed was you have variables a, b, and c. But what are they? What do they represent? Variable names should always be indicative of what they do. Also, comments would help

Here is a start ...

  1. int main()
  2. {
  3. int input, even=0, odd=0;
  4. do
  5. {
  6. cout << "Please enter a positive integer (negative integer to stop):"<< ' ';
  7. cin>> input;
  8. if ( (even%2 == 0) && (input >= 0) )
  9. even++;
  10. else if (input >= 0)
  11. odd++;
  12. }
  13. while (input>=0);
  14.  
  15. cout << "You have entered" << ' '<< odd << ' ' <<"odd numbers." << endl;
  16. cout << "And" << ' ' << even << ' ' <<"even numbers." << endl;
  17. system("PAUSE");
  18. return 0;
  19. }

Note that my if statement has a conditional to make sure not to include the negative number. This is the easiest quick fix considering you're using a do while loop like this. You could also just use a nested if statement.

Another way to do it is to prompt the user to enter a first number. And then from then on use a while loop instead of a do while loop, where the prompt to enter a number is on the last line of the loop.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Mar 2004
Posts: 2
Reputation: c++help is an unknown quantity at this point 
Solved Threads: 0
c++help c++help is offline Offline
Newbie Poster

Re: I need some C++ help

 
0
  #3
Mar 23rd, 2004
thanks for the help, I really appreciate it. I have been really struggling with this language. I look forward to being a part of your community.
Reply With Quote Quick reply to this message  
Join Date: Feb 2002
Posts: 12,040
Reputation: cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light cscgal is a glorious beacon of light 
Solved Threads: 128
Administrator
Staff Writer
cscgal's Avatar
cscgal cscgal is offline Offline
The Queen of DaniWeb

Re: I need some C++ help

 
0
  #4
Mar 23rd, 2004
Hey there. That's great. Welcome aboard.
Dani the Computer Science Gal
Follow my Twitter feed! twitter.com/daniweb
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 1
Reputation: mr.HAMZA is an unknown quantity at this point 
Solved Threads: 0
mr.HAMZA mr.HAMZA is offline Offline
Newbie Poster

I get confuse!

 
0
  #5
20 Days Ago
Hi, I'm HAMZA
I need help in java program & I have the flow chart + formula
I get confuse when I face this Problem So I ask 4 help...
contact me immediately at : hamza399@hotmail.com
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC