How would I make this into a while loop?

closed account (4wRjE3v7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
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
Does this answer your question?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>

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.