How do you write a function to randomly pick two integers and a character( /, * ,- ,+ ,%) as it's parameters and return the result of the operator.
This is a discussion on random character within the C++ Programming forums, part of the General Programming Boards category; How do you write a function to randomly pick two integers and a character( /, * ,- ,+ ,%) as ...
How do you write a function to randomly pick two integers and a character( /, * ,- ,+ ,%) as it's parameters and return the result of the operator.
Research the rand() function. Research typecasting.
Start here:
http://www.cprogramming.com/cboard/s...&threadid=2366
Hell, I'm tired and stupid, why don't I do it for you?
Of course you may want to add error checking and such (like if one int is larger/smaller then the other.Code:int DoSomething() { srand(unsigned(time(NULL))); int integers[2]; integers[0] = rand(); integers[1] = rand(); char cOperatorArray[] = "+-*/%"; switch (cOperatorArray[rand() % (4 + 1)]) { case '+': return integers[0] + integers[1]; case '-': return integers[0] - integers[1]; case '*': return integers[0] * integers[1]; case '/': return integers[0] / integers[1]; case %: return integers[0] % integers[1]; default: return 0; } }