Functions & Switches

Im working on an assignment for school, and Im having trouble grasping the concepts of functions fully.
The idea of the program is to run a menu based program that asks the user if they want to perform a summation, exponential, factorial functions or exit the program. The program runs until the user chooses the exit function.
I have to use separate prototypes, and definitions instead of declaring and defining in one statement.
Thanks

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
 /*
 * 
 * 04.06.2014
 * Program 6, Chapter 6, Revision of Chp 5 - Page 294, #1
 *
 * This program asks the user to select a menu choice.
 * Once the user makes a choice the user will enter an input.
 * The input is validated, and based on the menu choice 1,2, or 3
 * the program will provide an arithmetic operation to the input
 * and display its result.
 * If the user selects 4, the program will end.
 * (Edit)
 * The new program will incorporate the aforementioned
 * but will now incorporate the use of functions to
 * carry out the necessary commands.
 */

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int menu();
long int getSum();
long int getFactor();
long int getExpo();
string exit();


int main()
{
	const int SUM = 1, // Summation choice
    FACTOR = 2, // Factorial choice
    EXPO = 3, // Exponential choice
    QUIT = 4; // Exit choice
    
	
    int choice; // Numerical menu choice
    long int sums, facts, expos;
    string quits;
    
    
	// Force the console to print standard notation
	cout << fixed << setprecision(0);
    
	// Do-While loop controlled by the users choices
	do
	{
		choice = menu();
        
        
        
		switch (choice) // Start switch option
		{
            case SUM: // 1st choice
                sums = getSum();
                break;
                
            case FACTOR: // 2nd choice
                facts = getFactor();
                break;
                
            case EXPO: // 3rd choice
                expos = getExpo();
                break;
                
            case QUIT: // 4th choice
                quits = exit();
                break;
                
            default: // Default choice
                // Error message for input outside domain
                cout << "Please make a selection of either 1,2,3 or 4.\n\n";
                cin >> choice; // Repeat attempt to gather input from user
		}
	}
	while (menu() != QUIT);
    
    return 0;
}

int menu()
{
    int choice;
    cout << "\n\t\tMathematical Menu\n" // Header
    << "1) Summation\n" // 1st choice
    << "2) Factorial\n" // 2nd choice
    << "3) Exponential\n" // 3rd choice
    << "4) Exit Program\n\n" // 4th choice
    << "Please make a selection\n" // Ask user for imput choice
    << "of either 1,2,3 or 4.\n\n";
    cin >> choice; // Gather input from user
    return choice;
}

long int getSum()
{
    int total = 0, userNum, counter;
    
    // Ouput statement to user
    cout << "Please enter a positive integer value greater than 0 \n"
    << "and less than 10,000,000.\n\n";
    cin >> userNum; // Repeat attempt to gather input from user
    
    // Compare input to domain
    if (userNum < 0 || userNum > 10000000)
    {
        // Error message for input outside domain
        cout << "Please check your entry and try again.\n\n";
        cin >> userNum; // Repeat attempt to gather input from user
    }
    
    // Perform arithmetic summation
    for (counter = 1; counter <= userNum; counter++)
    {
        total += counter; // Running count
    }
    cout << "The total value for the added numbers 1 to \n"
    << userNum << " is:\n"<<total;
    return total;
}

long int getFactor()
{
    int total, userNum, counter;
    
    total = 1;
    // Output statement to user
    cout << "Please enter a positive integer from 0 \n"
    << "and less than 100.\n\n";
    cin >> userNum; // Gather input from user
    
    // Compare input to domain
    if (userNum > 100 || userNum < 0)
    {
        // Error message if input is outside domain
        cout << "Please check your entry and try again.\n\n";
        cin >> userNum; // Repeat attempt to gather input from user
    }
    
    // Perform arithmetic factorial
    for (counter = 1; counter <= userNum; counter++)
    {
        total *= counter; // Running count
    }
    
    // Display arithmetic output to user
    cout << "The total value for the multiplied numbers 1 to \n"
    << userNum << " is:\n";
    return total;
}

long int getExpo()
{
    int total, userNum, counter;
    
    total = 0;
    // Output statement to user
    cout << "Please enter a positive integer from 0 \n"
    << "and less than 100.\n\n";
    cin >> userNum; // Gather input from user
    
    // Compare input to domain
    if (userNum > 100 || userNum < 0)
    {
        // Error message if input is outside domain
        cout << "Please check your entry and try again.\n\n";
        cin >> userNum; // Repeat attempt to gather input from user
    }
    
    // Perform arithmetic exponential
    for (counter = 1; counter <= userNum; counter++)
    {
        total = pow(2.0, userNum); // Running count
    }
    
    // Display arithmetic output to user
    cout << "The total value for the exponential function is \n";
    return total;
}

string exit()
{
    // Exit message
    return "Don't be gone for too long...\n";
    
}
Last edited on
Topic archived. No new replies allowed.