Default command at the end of my cases doesn't work

In the ninth line from the bottom my default: cout<<"That is not an option!;" won't read out. I was hoping someone with more than my meager 8 days of programing experience could shed some light on it. Thank you in advance.


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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#include <cstdlib>
#include <iostream>
#include <cmath>
#define pi 4 * atan(1)
using namespace std;

int main()
{
  float var;
  float var2;
  char function;

  for (;;)
  {

    do
    {
      cout << "Choose an operation, or press x to quit. \n";
      cout << "a - Addition \n";
      cout << "b - Subtraction \n";
      cout << "c - Multiplacation \n";
      cout << "d - Division \n";
      cout << "e - The value of e \n";
      cout << "f - The square of a number \n";
      cout << "g - Take the ln of a number \n";
      cout << "h - x^y \n";
      cout << "i - Square root \n";
      cout << "j - Take a cube root \n";
      cout << "k - Value of Pi \n";
      cout << "l - Reciprical of a number \n";
      cout << "Trig Functions are in Degrees and Radians.\n";
      cout << "m - sin \n";
      cout << "n - cos \n";
      cout << "o - tan \n";
      cout << "p - csc \n";
      cout << "q - sec \n";
      cout << "r - cot \n";
      cout << "s - Calculate compound intrest \n";
      cout << "t - Calculate the roots of a quadratic equation \n";
      cout << 
        "u - Calculate the answer to life the universe and everything. \n\n";

      cin >> function;

    }
    while (function < 'a' || function > 'u' && function != 'x');

    if (function == 'x')
      break;

    switch (function)

    {
      case 'a':
        cout << "Enter a number\n";
        cin >> var;
        cout << "Enter number to be added\n";
        cin >> var2;
        cout << var << "+" << var2 << " is " << var + var2 << "\n\n";
        break;

      case 'b':
        cout << "Enter a number\n";
        cin >> var;
        cout << "Enter the number to be subtracted\n";
        cin >> var2;
        cout << "The difference of " << var << "-" << var2 << " is " << var -
          var2 << "\n\n";
        break;

      case 'c':
        cout << "Enter a number to multiply\n";
        cin >> var;
        cout << "Enter the other number\n";
        cin >> var2;
        cout << "The result of " << var << "*" << var2 << " is " << var *var2
          << "\n\n";
        break;

      case 'd':
        cout << "Enter a number\n";
        cin >> var;
        cout << "Enter the divisor \n";
        cin >> var2;
        cout << "The quotient of " << var << "/" << var2 << " is " << var /
          var2 << "\n\n";
        break;

      case 'e':
        cout << "The Value of e is 2.71828182845904523536\n\n";
        break;

      case 'f':
        cout << "Enter a number to square\n";
        cin >> var;
        cout <<var << " squared is " << var * var << ".\n\n";
        break;

      case 'g':
        cout << "Enter a number\n";
        cin >> var;
        cout << "The ln is " << log(var) << "\n\n";
        break;

      case 'h':
        cout << "Enter a number x \n";
        cin >> var;
        cout << "Enter the exponent y \n";
        cin >> var2;
        cout << "The result of x^y is "<< pow(var, var2) << "\n\n";
        break;

      case 'i':
        cout << "Enter a number\n";
        cin >> var;
        cout << "The square root of " <<var<< " is " <<sqrt(var) << "\n\n";
        break;

      case 'j':
        double cube, cuberoot;
        cout << "Enter a number \n";
        cin >> cube;
        cuberoot = pow(abs(cube), 1.0 / 3.0);
        cout << "\nThe cube root of " << cube << " is " << cuberoot << "\n\n";
        break;

      case 'k':
        cout << "The value of pi is 3.14159265358979323846264338327950288\n\n";
        break;

      case 'l':
        cout << "Please enter a number\n";
        cin >> var;
        cout << "The reciprical of the number is " << (1 / var) << "\n\n";
        break;

      case 'm':
        double select;
        double trig, degRads, rad;
        cout << "Enter 1 for degrees and 2 for radians. \n";
        cin >> select;

        if (select == 1)
        {
          cout << "Enter the angle in degrees.\n";
          cin >> trig;
          degRads = ((trig) *pi) / 180;
          cout << "The sine is " << sin(degRads) << "\n\n" << endl;
        }
        else if (select == 2)
        {
          cout << "Enter the angle in radians.\n";
          cin >> rad;
          cout << "The sine is " << sin(rad) << "\n\n";
        }
        else if (select != 1 || select != 2)
        {
          cout << "That is not an option\n\n";
        }
        break;

      case 'n':
        cout << "Enter 1 for degrees and 2 for radians. \n";
        cin >> select;

        if (select == 1)
        {
          cout << "Enter the angle in degrees.\n";
          cin >> trig;
          degRads = ((trig) *pi) / 180;
          cout << "The cosine is " << cos(degRads) << "\n\n";
        }
        else if (select == 2)
        {
          cout << "Enter the angle in radians.\n";
          cin >> rad;
          cout << "The cosine is " << cos(rad) << "\n\n";
        }
        else if (select != 1 || select != 2)
        {
          cout << "That is not an option\n\n";
        }
        break;
        
      case 'o':
        cout << "Enter 1 for degrees and 2 for radians. \n";
        cin >> select;

        if (select == 1)
        {
          cout << "Enter the angle in degrees.\n";
          cin >> trig;
          degRads = ((trig) *pi) / 180;
          cout << "The tangent is " << tan(degRads) << "\n\n";
        }
        else if (select == 2)
        {
          cout << "Enter the angle in radians.\n";
          cin >> rad;
          cout << "The tangent is " << tan(rad) << "\n\n";
        }
        else if (select != 1 || select != 2)
        {
          cout << "That is not an option\n\n";
        }
        break;
//I took out these functions so I could post the code

      case 's':
        double percent, time, number, rate, totalComp, yield;
        cout << "This will calculate compound intrest.\n";
        cout << "Enter the principal in dollars and cents: ";
        cin >> percent;
        cout << "Enter the time in years: ";
        cin >> time;
        cout << "Enter the number of compoundings a year: ";
        cin >> number;
        cout << "Enter the intrest rate as a percent: ";
        cin >> rate;
        totalComp = number * time;
        yield = percent * pow((1+(rate *0.01) / number), (totalComp)) - percent;
        cout << "The total intrest yield is: " << yield << "\n\n";
        break;

      case 't':
        double value, value2, value3, value4, value5, value6, value7, value8,
          value9;
        cout << 
          "This will calculate roots of a quadratic equation of the form ax^2+bx+c \n";
        cout << "Enter the leading coefficient 'a': ";
        cin >> value;
        cout << "Enter coefficient 'b': ";
        cin >> value2;
        cout << "Enter the constant 'c': ";
        cin >> value3;
        value6 = (2 *value);
        value7 = (value2 *value2) - 4 * value * value3;
        value8 =  - 1 * value7;
        value9 =  - 1 * value2;
        if (value7 < 0)
        {
          value5 = sqrt(value8);
          cout << "The roots are " << (value9 + value5) / value6 << "i and " <<
            (value9 - value5) / value6 << "i\n\n";
        }
        else
        {
          value4 = sqrt(value7);
          double value11 = ( - value + value4) / value6;
          cout << "The roots are " << (value9 + value4) / value6 << " and " << 
            (value9 - value4) / value6 << "\n\n";
        }
        break;

      case 'u':
        cout << "The answer is 42. \n\n";
        break;

      default://This is the problem
        cout << "That is not an option!";//this is the problem
    }

  }

  return 0;

}
Last edited on
Well, your while loop won't let you enter anything outside the range ['a';'u']+'x', and your switch is already covering all those possibilities in the switch, so it's not that it's not printing it, it's that you're still inside the loop. You can try modifying your condition to allow 'v', but I think not allowing the user to enter something is better than allowing it and later telling them it's not allowed.
hey i'm also learning the language. i compiled this code and found out the do-while loop is the problem. just remove the lines 16, 17, 45, 46 and it should work since you're a using break anyway.

1
2
    if (function == 'x')
      break;
Thank you, both solutions worked.
i love helping others but i hate switch statement. lol
Topic archived. No new replies allowed.