944,008 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 58081
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Mar 23rd, 2004
0

Loop counting odd and even numbers

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

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
c++help is offline Offline
2 posts
since Mar 2004
Mar 23rd, 2004
0

Re: I need some C++ help

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 ...

C++ Syntax (Toggle Plain Text)
  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.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Mar 23rd, 2004
0

Re: I need some C++ help

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
c++help is offline Offline
2 posts
since Mar 2004
Mar 23rd, 2004
0

Re: I need some C++ help

Hey there. That's great. Welcome aboard.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Oct 2nd, 2010
0
Re: Loop counting odd and even numbers
can you help me make a code on arrays in finding even and odd numbers
Reputation Points: 10
Solved Threads: 0
Newbie Poster
avaughn is offline Offline
1 posts
since Oct 2010
Oct 2nd, 2010
0
Re: Loop counting odd and even numbers
Hi avaughn, welcome to DaniWeb!

If you look at the post by cscgal above, you will see all of the elements that you need. The "if number%2 == 0" test is how to tell if a number is even or odd. Then you just have to keep track of which ones are which! Please give it a try and post some code and we can help if you get stuck.

David
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Oct 4th, 2010
0

Please help me !!

please help me how to determine odd and even number and how can i count all the odd or even numbers?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mcroneil is offline Offline
1 posts
since Oct 2010
Oct 4th, 2010
0
Re: Loop counting odd and even numbers
i need some help about the bubble sort. i dont knw how to start.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
rheg is offline Offline
1 posts
since Oct 2010
Oct 4th, 2010
0
Re: Loop counting odd and even numbers
@mcroneil - Please show us your attempt and we can help you debug it.

@rheg - You should start a new thread when you have a new question.
Featured Poster
Reputation Points: 437
Solved Threads: 204
Posting Virtuoso
daviddoria is offline Offline
1,968 posts
since Feb 2008
Oct 8th, 2010
0
Re: Loop counting odd and even numbers
Okay so I'm taking a computer science class and I have no idea what I'm doing! The hmwk is to design a program that iterates through integers from 1 to a value determined by user and add the odd numbers in the range and the even numbers in the range and output the sum for both. I am clueless as to how to get the program to add only the odds together and only the evens together... please help. This is what I have thus far...

#include <iostream>
using namespace std;
void main(void)
{
int number, max, increment;

cout << "Enter the max value: ";
cin >> max;
cout << "Enter the increment: ";
cin >> increment;

for(number = 1; number <= max; number = number + increment)
{
cout << number << endl;

if((number%2) == 0)
{
cout << "This number is even." << endl;
}
else if((number%2) == 1)
{
cout << "This number is odd." << endl;
}
else
{
cout << "Error." << endl;
}
}
}
Reputation Points: 10
Solved Threads: 0
Newbie Poster
clueless215 is offline Offline
1 posts
since Oct 2010

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: MFC - Closing and Opening Dialog box
Next Thread in C++ Forum Timeline: Open a dialog box in MFC using pointer





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


Follow us on Twitter


© 2011 DaniWeb® LLC