Function with if-statements not working.

This is a discussion on Function with if-statements not working. within the C Programming forums, part of the General Programming Boards category; I've come up with the following function to find out which type a card is. Code: char color(int random) { ...

  1. #1
    Novice.
    Join Date
    Oct 2005
    Posts
    88

    Function with if-statements not working.

    I've come up with the following function to find out which type a card is.
    Code:
    char color(int random)
    {
    	char colour;
    	if (random < 13)
    	{
    		colour = 'S'; //Spades
    	}
    	if ((random > 12) && (random < 25))
    	{
    		colour = 'C' ;//Clubs
    	}
    	if ((random > 24) && (random < 39))
    	{
    		colour = 'D'; //Diamonds
    	}
    	if (random > 38)
    	{
    		colour = 'H'; //Hearts
    	}
    	else
    	{
    		colour = 'E'; //Error!
    		puts("***Error in char(colour)***");
    	}
    	return (colour);
    }
    It doesn't seem to work, since it always prints the error code. Calling the function looks like this:
    Code:
    	random = select(deck); //Ontvangt de waarden van return (selected) uit void select.
    	colour = color(random);
    What is wrong?

    Thank you.

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    May-be you want else if-s. Right now the last if-else in your code says, if random is not more than 38, it's an error.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Super unModrator
    Join Date
    Dec 2007
    Posts
    321
    I think you need to print the error only when random<0, not when its not more than 38

  4. #4
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    It works now. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  3. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  4. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM

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