How incorporate this second method?

So i did the task of writing a code for an accumulator as seen here. Now i have to use a second method and am confused with how i exactly do it. How do i use the accumulate2 function? The instructions are:

Create the method main that has at least three variables: a double variable accumulator, a double variable operand, and a char variable operation. It does the following:

It initially assigns the value 0 to accumulator.
It inputs from the user the value of operation.
(valid values are +, -,*,/. The program ends when user inputs x).
It inputs from the user the value of operand.
It calls the method accumulate2 and pass it three parameters: accumulator, operand, and operation. It receives the return value in variable accumulator.
On return from the method accumulate, it displays the value returned in variable accumulator.
It repeats the above till the user inputs x when prompted for operation.

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

using namespace std;
void accumulate (double & accumulator, double opd, char op)
double accumulate2 (double acc, char op, double opd)


{
    switch (op)
    {
        case '+':
        accumulator = accumulator + opd;
        cout << "Accumulated Value: " << accumulator;
        break;

        case '-':
        accumulator = accumulator - opd;
        cout << "Accumulated Value: " << accumulator;
        break;

        case '*':
        accumulator = accumulator  * opd;
        cout << "Accumulated Value: " << accumulator;
        break;

        case '/':
        accumulator = accumulator / opd;
        cout << "Accumulated Value: " << accumulator;
        break;
    }
    cout << endl;
}
int main()
{
    double opd,accumulator = 0;
    char op;
    cout << "Accumulated Value: " << accumulator << endl;
    cout << endl;
    cout << "Next Operation: " << endl;
    cin >> op;
    while (op == '+' || op == '-' || op == '*' || op == '/')
    {
        cout << "Next Operand" << endl;
        cin >> opd;
        accumulate (accumulator,opd,op);
        cout << endl;
        cout << "Next Operation: " << endl;
        cin >> op;
    }
    cout<<"Bye"<<endl;

    return 0;
}
Last edited on
Topic archived. No new replies allowed.