Separating main into different functions

I need help separating some of the functions from main and making them their own independent functions. Any help is appreciated.

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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  #include <iostream>
#include <math.h>
using namespace std;

/**
 * Provides a command line calculator. Allows unary (e.g., sin, cos, tan) and
 * binary (e.g., +, -, /) operators.
 *
 * @return The exit status; 0 means success, 1 means bad input.
 */
int main()
{
    char operatorType;
    string theOperator;
    float operand1, operand2, result;

    // Prompt the user for the type of calculation to perform.
    cout << "Hi! Would you like to use a [b]inary or [u]nary operator? ";
    cin >> operatorType;
}


    // Parse the operator type.
    // Binary operator.
    if( operatorType == 'b' )
    {
        // Read the operands and operator from the user.
        cout << "Please enter the first operand: ";
        cin >> operand1;

        cout << "Please enter the operator [*,-,/,+,^]: ";
        cin >> theOperator;

        cout << "Please enter the second operand: ";
        cin >> operand2;

        // Parse the operator.
        if( theOperator == "*" )
        {
            result = operand1 * operand2;
        }
        else if( theOperator == "/" )
        {
            result = operand1 / operand2;
        }
        else if( theOperator == "+" )
        {
            result = operand1 + operand2;
        }
        else if( theOperator == "-" )
        {
            result = operand1 - operand2;
        }
        else if( theOperator == "^" )
        {
            result = pow(operand1, operand2);
        } 
        else
        {
            cout << "You entered an invalid binary operator." << endl;
            return 1;
        }

        // Output the result.
        cout << "Result: " << result << endl;
    }
    // Unary operator.
    else if( operatorType == 'u')
    {
        // Read the operand and operator from the user.
        cout << "Please enter the operator [sin,cos,tan,-]: ";
        cin >> theOperator;

        cout << "Please enter the operand: ";
        cin >> operand1;

        // Parse the operator.
        if( theOperator == "sin" )
        {
            result = sin(operand1);
        }
        else if( theOperator == "cos" )
        {
            result = cos(operand1);
        }
        else if( theOperator == "tan" )
        {
            result = tan(operand1);
        }
        else if( theOperator == "-" )
        {
            result = -operand1;
        }
        else
        {
            cout << "You entered an invalid unary operator." << endl;
            return 1;
        }

        // Output the result.
        cout << "Result: " << result << endl;
    }
    // Bad input.
    else
    {
        cout << "You entered an invalid operator type." << endl;
        return 1;
    }

    return 0;
}
Have you mapped out in your head or on paper what functions you will need to do what?
Topic archived. No new replies allowed.