random character

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 ...

  1. #1
    Unregistered
    Guest

    random character

    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.

  2. #2
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    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?

    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;
    	}
    }
    Of course you may want to add error checking and such (like if one int is larger/smaller then the other.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf returns random character
    By giannisapi in forum C Programming
    Replies: 3
    Last Post: 06-20-2009, 12:06 PM
  2. character set translation
    By password636 in forum C Programming
    Replies: 1
    Last Post: 06-08-2009, 11:45 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Random character
    By rusty0412 in forum C++ Programming
    Replies: 4
    Last Post: 06-27-2003, 06:29 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21