Genki All American 590 Posts user info edit post |
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 |
philihp All American 8349 Posts user info edit post |
what? 10/5/2007 2:57:53 PM |
sober46an3 All American 47925 Posts user info edit post |
add a ; to the end of each line. 10/5/2007 2:59:14 PM |
msb2ncsu All American 14033 Posts user info edit post |
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; }
[Edited on October 5, 2007 at 3:25 PM. Reason : cmmt]10/5/2007 3:22:05 PM |
sarijoul All American 14208 Posts user info edit post |
^why are you gonna do this guy's homework for him? 10/5/2007 3:35:09 PM |
Wraith All American 27257 Posts user info edit post |
What language? 10/5/2007 3:46:07 PM |
Genki All American 590 Posts user info edit post |
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 |
Genki All American 590 Posts user info edit post |
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 |
cyrion All American 27139 Posts user info edit post |
i dont see why you couldnt though ive never tried it. 10/5/2007 7:33:40 PM |
msb2ncsu All American 14033 Posts user info edit post |
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 |
philihp All American 8349 Posts user info edit post |
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 |