Need help approaching some coding assignments.

-Write a program using while loops that prompts the user for a set of integers.
-Your program will ask for one integer at a time. If the number 0 is entered, the list is complete and your program will output two results: the sum of the even integers and the sum of the odd integers.
-After each integer is entered by the user, your program will print out whether the integer entered by the user is less than, equal to, or greater than the previous integer. Assume for the first integer entered by the user that the “previous integer” was 0.
-Your program must handle a variable number of inputs (in other words, you cannot hard code for the test cases).

This is what I have so far:

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
26
27
28
29
#include <iostream>

using namespace std;

int main()
{
    int a;
    int b;
    
    a = 1;
    b = 0;
    
    while (a != 0) {
        cout << "Please enter an integer: ";
        cin >> a;
        
        if (a > b) {
            cout << "New integer is greater than previous." << endl;}
        if (a < b) {
            cout << "New integer is less than previous." << endl;}
        if (a = b) {
            cout << "new integer is equal to previous." << endl;}
        }
            
        a = b;
    }

return 0;
}


Any advice or tips are greatly appreciated.
Last edited on
For anyone wondering, I figured it out.

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>

using namespace std;

int main()
{
    int a;
    int b;
    int sum_odds;
    int sum_evens;
    
    a = 1;
    b = 0;
    sum_odds = 0;
    sum_evens = 0;
    
    while (a != 0) {
        cout << "Please enter an integer: ";
        cin >> a;
        
        if (a % 2 == 0) {
            sum_evens = sum_evens + a;
        }
        
        if (a % 2 != 0) {
            sum_odds = sum_odds + a;
        }
            
        if (a >= b) {
            if (a > b) {
                cout << "The integer is greater than the previous." << endl;
            }
            else { 
                cout << "The integer is equal to the previous." << endl;
            }
        }
        
         if (a < b) {
            cout << "The integer is less than the previous." << endl;
        }
        
        b = a;
        
    }
    
    cout << "The total of the even numbers is " << sum_evens << "." << endl;
    cout << "The total of the odd numbers is " << sum_odds << "." << endl;

return 0;
}
Here I have come with a bit of the changes
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>

using namespace std;

int main()
{
    int a;
    int b;
    int sum_odds;
    int sum_evens;

    a = 1;
    b = 0;
    sum_odds = 0;
    sum_evens = 0;
    bool flagfirst=0;
    while (a != 0) {
        cout << "Please enter an integer: ";
        cin >> a;

        if (a % 2 == 0) {
            sum_evens = sum_evens + a;
        }

        if (a % 2 != 0) {
            sum_odds = sum_odds + a;
        }
        if(flagfirst==0 || a==0)
        {
            flagfirst=1;
            b=a;
        }
        else
        {

                if (a >= b) {
            if (a > b) {
                cout << "The integer is greater than the previous." << endl;
            }
            else {
                cout << "The integer is equal to the previous." << endl;
            }
        }

         if (a < b) {
            cout << "The integer is less than the previous." << endl;
        }
        }
        b = a;

    }

    cout << "The total of the even numbers is " << sum_evens << "." << endl;
    cout << "The total of the odd numbers is " << sum_odds << "." << endl;

return 0;
}
Topic archived. No new replies allowed.