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:

Enter a sequence of numbers (0 to quit): 12 4 19 3 0

12 is even
4 is even
19 is odd
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,

while (x != 0)

But, it just looped "12 is even" over and over.

Here's my code.

int main ()
{
   int input;
   cout <<"Enter a sequence of numbers (0 to quit): ";
   cin >> x;

   if (x != 0)
   {
     if (x % 2 == 0)
      {
         cout << x << " is even" << endl;
      }
      else if (x % 2 != 0)
      {
         cout << x << " isn't even" << endl;

      }
  }
}

Ok your code has a few problems with it. First put this at the top of the code

#include <iostream>
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.

Comments
You're awesome, thanks!

Here's my code.

int main ()
{
   int input;
   cout <<"Enter a sequence of numbers (0 to quit): ";
   cin >> x;

   if (x != 0)
   {
     if (x % 2 == 0)
      {
         cout << x << " is even" << endl;
      }
      else if (x % 2 != 0)
      {
         cout << x << " isn't even" << endl;

      }
  }
}

Your almost there :

1) change : int input; TO int x;
2) Wrap your code in a do while loop.

int x = -1;
do
{
  /* get user input and check for even/oddity */
}while(x != 0) //quits if user enters 0
Comments
You rule! Thank you so much!

Thank you very much! The Do...While loop worked like a charm.

This article has been dead for over six months. Start a new discussion instead.