Jump to content

Switch Statements/returning value

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
10 replies to this topic

#1
melodia

melodia

    Newbie

  • Members
  • Pip
  • 7 posts
Hi,
I'm writing a program for blackjack and am using an array with and index of 53 with the first 52 spaces for the cards and the 53rd is for keeping track of how many cards are left in the "deck". There are several function prototypes and when I compile I only have a problem with the switch statement for "scoring the card"


int Score_Card(int card)

 {

     

     int rank=card%13 + 1;

     

     switch(rank)

     {

      case 1:

           return rank;

           break;

     

      case 2:

           return rank;

           break;

      

      case 3:

           return rank;

           break;

      

      case 4:

           return rank;

           break;

      

      case 5:

           return rank;

           break;

      

      case 6:

           return rank;

           break;

      

      case 7:

           return rank;

           break;

      

      case 8:

           return rank;

           break;

      

      case 9:

           return rank;

           break;

      

      case 10:

           return rank;

           break;

      

      case 11:

           return 10;

           break;

      

      case 12:

           return 10;

           break;

      

      case 13:

           return 10;

           break;

           }

     

}

I was under the impression that you could use this method of obtaining a return value through switch statements. However, the compiler says:

In function `Score_Card':
[Warning] control reaches end of non-void function
[Build Error] [blackjacknov12.o] Error 1

?????;)

is what this function says to do not allowed? Or am I missing some silly small thing??

#2
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts
I think it would be easier if you just had an if statement here

if (rank < 11) {
  return rank;
} else {
  return 10;
}

and the error must be that the compiler does not know that rank can't be larger than 13, so it believes there are a possibility for the function to not return anything. if you just put an "return 0;" as a last row, it would also work
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#3
melodia

melodia

    Newbie

  • Members
  • Pip
  • 7 posts
Thank you! :-D

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
The switch statement doesn't have an else, so is not guaranteed to return anything.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,298 posts

WingedPanther said:

The switch statement doesn't have an else, so is not guaranteed to return anything.

Yeah, forgot that C/C++ has an else on the switch statements, too used to PHP which has an case default: instead...
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#6
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts

WingedPanther said:

The switch statement doesn't have an else, so is not guaranteed to return anything.
...but C/C++ does have a default, else-like statement with Switch statements, called "default:".
switch (n)
{
    case 0:
    case 2:
    case 4:
    case 6:
    case 8:
        puts("someInt is Even.\n");
        break;
    case 1:
    case 3:
    case 5:
    case 7:
    case 9:
        puts("someInt is odd.\n");
        break;
    default:
        puts("Only single-digit numbers are allowed.");
}
However, as far as a better way to do this function, I'd use two if statements:
int Score_Card(int card)
{
    if (card > 0 && card < 52)
    {
        int rank = (card % 13) + 1;

        if (rank < 10) // This could also be < 11 with no ill effects.
            return rank;
        return 10;
    }

    return -1;
}

Wow I changed my sig!

#7
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Is there anything wrong with this?
int Score_Card(int card) {
    int rank = card % 13 + 1;
    switch(rank) {
        case 0 ... 10: 
            return rank;
            break;
        case 11 ... 13: 
            return 10; 
            break;
        default:
            printf("Error...\n");
    }
    return -1;  
}


#8
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

John said:

Is there anything wrong with this?
int Score_Card(int card) {

    int rank = card % 13 + 1;

    switch(rank) {

        case 0 ... 10: 

            return rank;

            break;

        case 11 ... 13: 

            return 10; 

            break;

        default:

            printf("Error...\n");

    }

    return -1;  

}
As gcc puts it...

Quote

warning: range expressions in switch statements are non-standard
And there is no need to break after you've already returned. And there will never be a case 0.

#9
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts

dcs said:

And there is no need to break after you've already returned. And there will never be a case 0.
Actually there is a case where 0 is returned, and that's when card is a multiple of -13 minus 1, so essentially this pattern: -1, -14, -27, -40, -53, etc. In each of these cases it will return 0, which it shouldn't.

You can test this with this code:
#include <stdio.h>


int Score_Card(int);


int main(void)

{

    printf("%d\n%d\n%d\n%d\n%d\n", Score_Card(-1),

                                   Score_Card(-14),

                                   Score_Card(-27),

                                   Score_Card(-35), // This should printf "Error..."

                                   Score_Card(-40));

    return 0;

}


int Score_Card(int card)

{

    int rank = card % 13 + 1;

    switch(rank) {

        case 0 ... 10: 

            return rank;

            break;

        case 11 ... 13: 

            return 10; 

            break;

        default:

            printf("Error...\n");

    }

    return -1;  

}
But if you were going to accept decks with greater than 52 cards, you'd only need one if statement:
#include <stdio.h>


short Score_Card(unsigned int);


int main(void)

{

    printf("%d\n%d\n%d\n%d\n%d\n", Score_Card(1), // Should be 2

                                   Score_Card(0), // Should be 1

                                   Score_Card(52), // Should be 1

                                   Score_Card(51), // Should be 10

                                   Score_Card(103)); // Should be 10

    return 0;

}


short Score_Card(unsigned int card)

{

    short rank = (card % 13) + 1;

    if (rank > 10)

        return 10;


    return rank;

}
Seems to work well.
Wow I changed my sig!

#10
John

John

    Writes binary right handed and hex left handed

  • Moderators
  • 6,321 posts
Are you compiling it with flags? I don't get any warnings?

#11
dcs

dcs

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 775 posts

ZekeDragon said:

Actually there is a case where 0 is returned, and that's when card is a multiple of -13 minus 1, so essentially this pattern: -1, -14, -27, -40, -53, etc. In each of these cases it will return 0, which it shouldn't.
Icky. The % operator with negative numbers? I pretty much just avoid that, because I glaze over when I try to read this:

Quote

When integers are divided and the division is inexact, if both operands are positive the result of the / operator is the largest integer less than the algebraic quotient and the result of the % operator is positive. If either operand is negative, whether the result of the / operator is the largest integer less than the algebraic quotient or the smallest integer greater than the algebraic quotient is implementation-defined, as is the sign of the result of the % operator. If the quotient a/b is representable, the expression (a/b)*b + a%b shall equal a.
But I gotta give you credit for catching me on that one; I missed it.

[edit]
----------------------------------------------------------------------------------------------------------------------------------
And now a completely different reply that for some reason a moderator felt obligated to merge into this one.
----------------------------------------------------------------------------------------------------------------------------------
[/edit]

John said:

Are you compiling it with flags? I don't get any warnings?
With gcc, I like to check things with -Wall -ansi -pedantic quite often. If you compiled that with gcc, you're probably using one of many Gnu extensions.

Edited by dcs, 17 November 2009 - 08:29 AM.