### How would I make this into a while loop?

closed account (4wRjE3v7)
 ``12345678910111213141516171819202122232425`` ``````#include using namespace std; int main() { int num; cout << "Please enter an integer\n"; cin >> num; if (num ==0) { cout << "Your integer is zero\n:"; } else if (num % 2 == 0) { cout << "Your integer number is even\n"; } else { cout << "Your integer is odd\n"; } cout << "Thanks for using our integer program!\n"; }``````

I have to write a program that asks the same thing: for the user to enter a number and keeps a running total of the number of even and odd numbers seen. If the user enters 0, the program should print the total number of even and odd numbers and exit.
Last edited on

 ``12345678910111213141516171819202122232425`` ``````#include int main() { using namespace std; int num, evens, odds; cout << "Please enter an integer: "; while (cin >> num && num != 0) { if (num % 2 == 0) evens++; else odds++; cout << "Please enter another integer: "; } cout << "You have entered " << evens << << " even numbers and " << odds << << " odd numbers."; return 0; }``````
Last edited on
odds and evens are uninitialized.
Sorry that was a very amateur oversight.

Replace the declaration in the code with this:
`int num, evens = 0, odds = 0;`
closed account (4wRjE3v7)
Thanks Elidor:)!!
Topic archived. No new replies allowed.