943,985 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 426
  • C++ RSS
Nov 1st, 2009
0

Multiple Inputs

Expand Post »
I know this is probably a really easy question, but my teacher failed to mention how to do it, so here goes.

I'm making a simple program that tells a user if a sequence of given numbers are even or odd. Here's what the program should look like:

C++ Syntax (Toggle Plain Text)
  1. Enter a sequence of numbers (0 to quit): 12 4 19 3 0
  2.  
  3. 12 is even
  4. 4 is even
  5. 19 is odd
  6. 3 is odd

I've gotten the program made, except my output stops at "12 is even".
My question is, how do I get my program to loop through all of the numbers and stop at 0?
I tried using a while loop,
C++ Syntax (Toggle Plain Text)
  1. while (x != 0)
But, it just looped "12 is even" over and over.
Reputation Points: 10
Solved Threads: 0
Light Poster
Towely is offline Offline
40 posts
since Sep 2009
Nov 1st, 2009
0
Re: Multiple Inputs
Post the rest of the code. That seems like it should be working to me.
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008
Nov 1st, 2009
0
Re: Multiple Inputs
Here's my code.

C++ Syntax (Toggle Plain Text)
  1. int main ()
  2. {
  3. int input;
  4. cout <<"Enter a sequence of numbers (0 to quit): ";
  5. cin >> x;
  6.  
  7. if (x != 0)
  8. {
  9. if (x % 2 == 0)
  10. {
  11. cout << x << " is even" << endl;
  12. }
  13. else if (x % 2 != 0)
  14. {
  15. cout << x << " isn't even" << endl;
  16.  
  17. }
  18. }
  19. }
Reputation Points: 10
Solved Threads: 0
Light Poster
Towely is offline Offline
40 posts
since Sep 2009
Nov 1st, 2009
1
Re: Multiple Inputs
Ok your code has a few problems with it. First put this at the top of the code
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. using namespace std;
Next change you variable input to x. This is the variable that will be entered by the user and tested by the if statement.
Next xhange the first if to a while loop and move cin>>x into the while loop.
Then use your current if/else to test the input.
Finally put return 0; before the final right brace }
Your main problem was leaving cin>>x outside of the while loop. You need it in the loop so you can enter many numbers.
Reputation Points: 100
Solved Threads: 48
Posting Pro in Training
Grn Xtrm is offline Offline
495 posts
since Nov 2008
Nov 1st, 2009
1
Re: Multiple Inputs
Click to Expand / Collapse  Quote originally posted by Towely ...
Here's my code.

C++ Syntax (Toggle Plain Text)
  1. int main ()
  2. {
  3. int input;
  4. cout <<"Enter a sequence of numbers (0 to quit): ";
  5. cin >> x;
  6.  
  7. if (x != 0)
  8. {
  9. if (x % 2 == 0)
  10. {
  11. cout << x << " is even" << endl;
  12. }
  13. else if (x % 2 != 0)
  14. {
  15. cout << x << " isn't even" << endl;
  16.  
  17. }
  18. }
  19. }
Your almost there :

1) change : int input; TO int x;
2) Wrap your code in a do while loop.
C++ Syntax (Toggle Plain Text)
  1. int x = -1;
  2. do
  3. {
  4. /* get user input and check for even/oddity */
  5. }while(x != 0) //quits if user enters 0
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,864 posts
since Dec 2008
Nov 1st, 2009
0
Re: Multiple Inputs
Thank you very much! The Do...While loop worked like a charm.
Reputation Points: 10
Solved Threads: 0
Light Poster
Towely is offline Offline
40 posts
since Sep 2009

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: calculator in c++ with string as input
Next Thread in C++ Forum Timeline: Need help with a simple while loop





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


Follow us on Twitter


© 2011 DaniWeb® LLC