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
188
189
190
191
192
193
194
195
196
197
/// @author Alexander Corey
/// @attention I pledge my word of honor that I have abided by the CSN
/// Academic Integrity Policy while completing this assignment.
/// @file pa04.cpp
/// @date 2017-10-01
/// @brief This program prompts the user to enter five numbers and then
/// presents them with a menu of number operations to choose from.
/// @note I spent approximately 4 to 5 hours developing this program.
/// I used the clear and ignore functions to restore the program back
/// to working order in the likely event that the user put it into a
/// fail state. I used Xcode for the majority of the program's
/// development which sped up the debugging process.

#include <iostream>
#include <string>

using namespace std;

int main()
{
    // Local Const variables
    const string SM_CHOICE      = "Smallest value";    // smallest value text
    const string LG_CHOICE      = "Largest value";     // largest value text
    const string SUM_CHOICE     = "Sum of values";     // sum of values text
    const string AVG_CHOICE     = "Average of values"; // avg of values text
    const string CHOICE_A       = "a.";                // choice a menu string
    const string CHOICE_B       = "b.";                // choice b menu string
    const string CHOICE_C       = "c.";                // choice c menu string
    const string CHOICE_D       = "d.";                // choice d menu string
    
    const string OPENING_PROMPT = "Please enter five numbers, separated by"
    " spaces"; // opening prompt text
    const string MENU_PROMPT    = "Please enter the letter of the choice you"
    " desire"; // menu prompt text
    const string RETRY_PROMPT   = "It doesn't look like anything to me."
    " Please try again."; // text that prompts user to run program again
    const string ERROR_MESSAGE  = "It doesn't look like anything to me."
    " Please restart the program."; // text for error message
    
    const string SM_VALUE_IS    = "The smallest value is ";    // text for
    // smallest value calculation message printed for user
    const string LG_VALUE_IS    = "The largest value is ";     // text for
    //largest value calculation message printed for user
    const string SUM_IS         = "The sum of values is ";     // text for
    // sum of values calculation message printed for user
    const string AVG_IS         = "The average of values is "; // text for
    // average of values value calculation message printed for user
    
    const char   period         = '.'; // period character for misc uses
    const char   space          = ' '; // space character for misc uses
    
    // Local variables
    
    double       userInput1     = 0.0; // holds first value input by user
    double       userInput2     = 0.0; // holds second value input by user
    double       userInput3     = 0.0; // holds third value input by user
    double       userInput4     = 0.0; // holds fourth value input by user
    double       userInput5     = 0.0; // holds fifth value input by user
    
    double       smallestValue;        // holds smallest value calculation
    double       largestValue;         // holds largest value calculation
    double       sumValues;            // holds sum of values calculation
    double       avgValues;            // holds average of values calculation
    
    char         menuChoice     = 0;   // holds menu choice input by user
    
    // INPUT
    cout << OPENING_PROMPT << "\n";
    cin >> userInput1 >>userInput2 >> userInput3 >> userInput4
    >> userInput5;
    
    // Checks input failure
    if (!cin)
    {
        // Restores program to working state if user input is invalid
        cin.clear();
        cin.ignore(200, '\n');
        
        cout << RETRY_PROMPT << "\n";
        cout << OPENING_PROMPT << "\n";
        cin >> userInput1 >>userInput2 >> userInput3 >> userInput4
        >> userInput5;
        if (!cin)
            cout << ERROR_MESSAGE << "\n";
        else {
            cout << MENU_PROMPT << "\n";
            cout << CHOICE_A << space << SM_CHOICE << "\n";
            cout << CHOICE_B << space << LG_CHOICE << "\n";
            cout << CHOICE_C << space << SUM_CHOICE << "\n";
            cout << CHOICE_D << space << AVG_CHOICE << "\n";
            cin >> menuChoice;
        }
    }
    else
    {
        cout << MENU_PROMPT << "\n";
        cout << CHOICE_A << space << SM_CHOICE << "\n";
        cout << CHOICE_B << space << LG_CHOICE << "\n";
        cout << CHOICE_C << space << SUM_CHOICE << "\n";
        cout << CHOICE_D << space << AVG_CHOICE << "\n";
        cin >> menuChoice;
    }
    
    // SWITCH - PROCESS AND OUTPUT
    
    // menu choice A/a logic
    switch (menuChoice) {
        case 'A':
            smallestValue = userInput1;
            if (smallestValue >= userInput2)
                smallestValue = userInput2;
            if (smallestValue >= userInput3)
                smallestValue = userInput3;
            if (smallestValue >= userInput4)
                smallestValue = userInput4;
            if (smallestValue >= userInput5)
                smallestValue = userInput5;
            
            cout << SM_VALUE_IS << smallestValue << period;
            break;
        case 'a':
            smallestValue = userInput1;
            if (smallestValue >= userInput2)
                smallestValue = userInput2;
            if (smallestValue >= userInput3)
                smallestValue = userInput3;
            if (smallestValue >= userInput4)
                smallestValue = userInput4;
            if (smallestValue >= userInput5)
                smallestValue = userInput5;
            
            cout << SM_VALUE_IS << smallestValue << period;
            break;
            // menu choice B/b logic
        case 'B':
            largestValue = userInput1;
            if (largestValue <= userInput2)
                largestValue = userInput2;
            if (largestValue <= userInput3)
                largestValue = userInput3;
            if (largestValue <= userInput4)
                largestValue = userInput4;
            if (largestValue <= userInput5)
                largestValue = userInput5;
            
            cout << LG_VALUE_IS << largestValue << period;
            break;
        case 'b':
            largestValue = userInput1;
            if (largestValue <= userInput2)
                largestValue = userInput2;
            if (largestValue <= userInput3)
                largestValue = userInput3;
            if (largestValue <= userInput4)
                largestValue = userInput4;
            if (largestValue <= userInput5)
                largestValue = userInput5;
            
            cout << LG_VALUE_IS << largestValue << period;
            break;
            // menu choice C/c logic
        case 'C':
            sumValues = userInput1 + userInput2 + userInput3 + userInput4
            + userInput5;
            cout << SUM_IS << sumValues << period;
            break;
        case 'c':
            sumValues = userInput1 + userInput2 + userInput3 + userInput4
            + userInput5;
            cout << SUM_IS << sumValues << period;
            break;
        case 'd':
            sumValues = userInput1 + userInput2 + userInput3 + userInput4
            + userInput5;
            avgValues = sumValues/5;
            cout << AVG_IS << avgValues << period;
            break;
            // menu choice D/d logic
        case 'D':
            sumValues = userInput1 + userInput2 + userInput3 + userInput4
            + userInput5;
            avgValues = sumValues/5;
            cout << AVG_IS << avgValues << period;
            break;
            
            // default menu choice
        default:
            if (cin) {
                cout << ERROR_MESSAGE;
                break;
            }
    }
    
    cout << endl;
    
    return 0;
}