How do I generate a random operator?

Can anyone post a short code on how to generate a random operator (*,-,+,/) please? I can't seem to figure out a way.
Thanks
Since you have 4 operators, can't you just generate a number between 1-4 and use the operator belonging to that number?
@TarikNeaj

That's what I had done originally however the code gets extremely long if I have an IF function for each possible number. For example

1
2
3
4
5
6
7
8
9
10
IF (operatorselector == 1){
cout << firstnum + secondnum;
input answer
IF (answer = firstnum + secondnum;
output congratulations you got it correct!
}

along with a bunch of other stuff in each IF statement. This would result in 4 of these large IF statements for each operator, which is not what I was looking for

You can create and use functions:
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
#include <iostream>
#include <cstdlib>
using namespace std;

int result(int num1, int num2, int result, int sign);


int main(){

// generate random number between 1 and 4
int sign = rand() % 4 + 1;
cout << " Sign generated is: " << sign << endl;

// ask user for info
int num1=0;
int num2=0;
int answer=0;
cout << " Enter 2 numbers (with a space between them): ";
cin >> num1 >> num2;

answer = result(num1, num2, answer, sign);
cout << endl << " Answer is: " << answer << endl;
return 0;

}

// function to do the math
int result(int num1, int num2, int answer, int sign)
{
   if (sign == 1)
    answer= num1 + num2;

   else if (sign ==2)
    answer= num1 - num2;

   else if (sign == 3)
    answer= num1*num2;

   else if (sign ==4)
    answer = num1/num2;

return answer;

}
Last edited on
Perhaps something like this using function pointers:

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
#include <iostream>

typedef double (*FunctionPointer)(double, double);

double plus(double a, double b) {
	return (a + b);
}

double minus(double a, double b) {
	return (a - b);
}

double multiply(double a, double b) {
	return (a * b);
}

double divide(double a, double b) {
	return (a / b);
}

int main() {

	enum Operator{ Plus = 0, Minus, Multiply, Divide };

	const int num_operators = 4;

	FunctionPointer operators[num_operators] = { plus, minus, multiply, divide };

	Operator index = Plus;
	/*
	int min = Operator::plus;
	int max = Operator::divide;
	index = random(min, max);
	*/

	double a = 10.0;
	double b = 5.0;

	double value = operators[index](a, b);

	std::cout << value << std::endl;

	return 0;
}


EDIT - even better, put the functions in their own unique namespace. <functional> provides binary function object classes with similar names in the standard namespace (which are also cool and worth looking into) - you wouldn't want to invoke any name clashing.
Last edited on
Perhaps a good old switch... I call this the Obama switch...


duh = rand*4

switch (duh){

case 1: I add pain everywhere I go...
break;
case 2: I divide people by race and religion
break;
case 3: I multiply hated towards republicans and white people
break;
case 4: I subtract freedoms from Americans every chance I get.
break;
default : Somethings wrong... Of yeah, Liberials are running wild.
}
@pearlyman: plase keep your political viewpoint out of other people's threads. If you want to talk politics, please create a thread in the lounge.
Didn't say it was true... just an example...
Topic archived. No new replies allowed.