Multiple Inputs

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2009
Posts: 40
Reputation: Towely is an unknown quantity at this point 
Solved Threads: 0
Towely Towely is offline Offline
Light Poster

Multiple Inputs

 
0
  #1
Nov 1st, 2009
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:

  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,
  1. while (x != 0)
But, it just looped "12 is even" over and over.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 467
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 39
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
0
  #2
Nov 1st, 2009
Post the rest of the code. That seems like it should be working to me.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 40
Reputation: Towely is an unknown quantity at this point 
Solved Threads: 0
Towely Towely is offline Offline
Light Poster
 
0
  #3
Nov 1st, 2009
Here's my code.

  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. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 467
Reputation: Grn Xtrm will become famous soon enough Grn Xtrm will become famous soon enough 
Solved Threads: 39
Grn Xtrm's Avatar
Grn Xtrm Grn Xtrm is offline Offline
Posting Pro in Training
 
1
  #4
Nov 1st, 2009
Ok your code has a few problems with it. First put this at the top of the code
  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.
Check out my new band URL on facebook. I'm the bass player. :) Become a fan and leave comments if you like.
URL on facebook!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,457
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 189
firstPerson's Avatar
firstPerson firstPerson is offline Offline
Nearly a Posting Virtuoso
 
1
  #5
Nov 1st, 2009
Originally Posted by Towely View Post
Here's my code.

  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.
  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
1) Prove that the area of a circle is pi*r^2, where "r" is the radius of the circle.
2) Problem 2[b]solved by : jonsca
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 40
Reputation: Towely is an unknown quantity at this point 
Solved Threads: 0
Towely Towely is offline Offline
Light Poster
 
0
  #6
Nov 1st, 2009
Thank you very much! The Do...While loop worked like a charm.
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the C++ Forum


Views: 189 | Replies: 5
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC