So I need help with this. I'm trying to create a program where the user selects the basic math functions(+, - , *, /). That part is fine but how would I go about lets say if the user selects the random option where it can pick from any of those four?
10/5/2007 2:42:49 PM
what?
10/5/2007 2:57:53 PM
add a ; to the end of each line.
10/5/2007 2:59:14 PM
enumerate the math types and call the random function in the standard library:
double RandomMath(double num1, double num2){ enum myMathCrap { ADD = 0, SUBTRACT = 1, MULTIPLY= 2, DIVIDE = 3 }; int iRandomInteger = rand()%4; // This gives you a random number between 0 and 3 double result = 0; switch (iRandomInteger) { case ADD: result = num1 + num2; break; case SUBTRACT: result = num1 - num2; break; case MULTIPLY: result = num1 * num2; break; case DIVIDE: result = num1 / num2; break; } return result;}
10/5/2007 3:22:05 PM
^why are you gonna do this guy's homework for him?
10/5/2007 3:35:09 PM
What language?
10/5/2007 3:46:07 PM
I'm not being graded on this. Just wanted to know how to do it. The language is in c++. thanks for the help.
10/5/2007 3:54:36 PM
Curious, I couldn't put a switch statement within another switch statement correct? I would prob have to use if/else.
10/5/2007 5:12:44 PM
i dont see why you couldnt though ive never tried it.
10/5/2007 7:33:40 PM
As long as you surround the nested switch statement in each case with brackets then it should be fine:
switch (num){ case 0: { switch (num2) { case 0: // do stuff break; case 1: // do other stuff break; default: break; } } case 1: { switch (num2) { case 0: // do crazy stuff break; case 1: // do more crazy stuff break; default: break; } } case 2: // do normal stuff break; default: break;}
10/5/2007 10:35:33 PM
you can put most any constructs within a switch block, as Duff's device demonstrated.
switch (count % 8) /* count > 0 assumed */ { case 0: do { *to = *from++; case 7: *to = *from++; case 6: *to = *from++; case 5: *to = *from++; case 4: *to = *from++; case 3: *to = *from++; case 2: *to = *from++; case 1: *to = *from++; } while ((count -= 8) > 0); }
10/6/2007 12:59:40 AM