Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Help in C Please!?!

  • 09-12-2004 11:16pm
    #1
    Closed Accounts Posts: 579 ✭✭✭


    For some reason I'm getting an error with my switch!, any help will be appreciated thanks :)


    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>

    struct card {
    const char *face;
    const char *suit;
    };

    typedef struct card Card;

    void fillDeck( Card * const, const char *[],
    const char *[] );
    void shuffle( Card * const );
    void deal( const Card * const );

    int main()
    {
    Card deck[ 52 ];
    const char *face[] = { "Ace", "Deuce", "Three",
    "Four", "Five",
    "Six", "Seven", "Eight",
    "Nine", "Ten",
    "Jack", "Queen", "King"};
    const char *suit[] = { "Hearts", "Diamonds",
    "Clubs", "Spades"};

    srand( time( NULL ) );

    fillDeck( deck, face, suit );
    shuffle( deck );
    deal( deck );
    return 0;
    }

    void fillDeck( Card * const wDeck, const char * wFace[],
    const char * wSuit[] )
    {
    int i;

    for ( i = 0; i <= 51; i++ ) {
    wDeck[ i ].face = wFace[ i % 13 ];
    wDeck[ i ].suit = wSuit[ i / 13 ];
    }
    }

    void shuffle( Card * const wDeck )
    {
    int i, j;
    Card temp;

    for ( i = 0; i <= 51; i++ ) {
    j = rand() % 52;
    temp = wDeck[ i ];
    wDeck[ i ] = wDeck[ j ];
    wDeck[ j ] = temp;
    }

    }
    int num;

    switch (j){

    case 1;
    {
    num=1;
    }

    case 2;
    {
    num=2;
    }
    case 3;
    {
    num=3;
    }
    case 4;
    {
    num=4;
    }
    case 5;
    {
    num=5;
    }
    case 6;
    {
    num=6;
    }
    case 7;
    {
    num=7;
    }
    case 8;
    {
    num=8;
    }
    case 9;
    {
    num=9;
    }

    case 10;
    {
    num=10;
    }
    case "Queen";
    {
    num=10;
    }
    case "King";
    {
    num=10;
    }
    }

    void deal( const Card * const wDeck )
    {
    int i;
    char reply;

    for ( i = 0; i <= 0; i++ )
    printf( "%5s of %-8s%c", wDeck[ i ].face,
    wDeck[ i ].suit,
    ( i + 1 ) % 2 ? '\t' : '\n' );


    printf("Would You Like Another Card? " );
    scanf("%c",&reply);

    if(reply='Y')
    {
    for ( i = 0;i<=0; i++ )
    printf( "%5s of %-8s%c", wDeck[ i+1 ].face,
    wDeck[ i +1].suit,
    ( i + 1 ) % 2 ? '\t' : '\n' );
    if(num+num!=21)
    printf("Would You Like Another Card? " );
    scanf("%c",&reply);

    if(reply='Y')
    {
    for ( i = 0;i<=0; i++ )
    printf( "%5s of %-8s%c", wDeck[ i+1 ].face,
    wDeck[ i +1].suit,
    ( i + 1 ) % 2 ? '\t' : '\n' );
    }

    }


Comments

  • Closed Accounts Posts: 158 ✭✭BrendanB


    You need to use colons as opposed to semi-colons, use breaks in each and every statement (otherwise C will fall-through the statements), and you must use numeric values as the string identifiers (no "Queen" etc.).
    e.g.
    case 2:
    {
    //..... Do stuff
    break;
    }


  • Closed Accounts Posts: 579 ✭✭✭Magnolia_Fan


    Didn't work still getting an error on switch a syntax error


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Any chance of seeing what the error says, or do we have to guess?


  • Closed Accounts Posts: 158 ✭✭BrendanB


    Sticking the switch statement into a function that it called and does something might help as well.


  • Registered Users, Registered Users 2 Posts: 7,278 ✭✭✭kenmc


    I suspect it is complaining about the strings being used for "Queen" and "King"
    (logic bug here by the way - what about Jacks??)
    Switches compare integers. It can compare the variable "j" to 1, 2, 3 etc. "Queen" does not translate to a string. If you wanna do this you should use just the letters J, Q, K for Jack Queen King and then the switch becomes
    case 1:
    //yada
    case 10:
    //break;
    case 'J':
    // whatever;
    case 'Q':
    // Something else
    // "King" is left as an exercise for the student.

    now I see that you're setting num to be 10 for the cases of 10, Queen and King (and I'll assume Jack)
    you can therefore do this.
    case 10:
    case 'J':
    case 'Q':
    num=10;
    break;

    have fun


  • Advertisement
  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    Lads, yer over complicating it, in the 'case' part he has semi-colon's instead of full colons.

    case 3;
    {
    num=3;
    }

    should be

    case 3:
    {
    num=3;
    }


  • Registered Users, Registered Users 2 Posts: 7,278 ✭✭✭kenmc


    well spotted. that'll teach me to have my resolution turned up so much! look the same from here!
    Anyway, the string problem will remain. but yeah - please post the compiler errors when asking for help.
    K


  • Closed Accounts Posts: 579 ✭✭✭Magnolia_Fan


    Actually I already changed the semi-colons and it didn't work but I'll be working for the next couple of days so I want get a chance to try the other suggestions but when I do I'll report back, Thanks for the help

    Plus the Compiler Error was 'Syntax Error - Switch'


  • Registered Users, Registered Users 2 Posts: 191 ✭✭Trine


    case 10;
    {
    num=10;
    }
    case "Queen";
    {
    num=10;
    }
    case "King";
    {
    num=10;
    

    I haven't run your code, but at a glance this looks wrong, the variable used in a switch statement can only be an int or char, but you're mixing them and trying to use a string.


  • Closed Accounts Posts: 579 ✭✭✭Magnolia_Fan


    I've changed them to char and put single quotes round the numbers but its still not working, anybody want to try and figure it out?, its supposed to be a game of 21 but its gone horribly wrong


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,038 ✭✭✭rob1891


    void shuffle( Card * const wDeck )
    {
    int i, j;
    Card temp;
    
    for ( i = 0; i <= 51; i++ ) {
    j = rand() % 52;
    temp = wDeck[ i ];
    wDeck[ i ] = wDeck[ j ];
    wDeck[ j ] = temp;
    }
    
    } // this brace closes the shuffle function!!
    int num;
    
    switch (j){
    
    case 1;
    { ....
    


    see where I have commented, the brace closes the shuffle function and the switch statement resides outside of any function, which it can't. There wasn't an error for "int num;" because it is okay ish to have a global variable in that position.

    hth

    Rob

    edit: do'h Brendan mentioned this already, I guess you didn't see it, as I missed his post too :)


  • Closed Accounts Posts: 579 ✭✭✭Magnolia_Fan



    Configuration: card3 - Win32 Debug
    Compiling...
    card3.c
    C:\card3\card3.c(66) : error C2059: syntax error : 'switch'
    Error executing cl.exe.

    card3.obj - 1 error(s), 0 warning(s)


    for:#include <stdio.h>
    #include <stdlib.h>
    #include <time.h>


    int num;
    struct card {
    const char *face;
    const char *suit;

    };

    typedef struct card Card;

    void fillDeck( Card * const, const char *[],
    const char *[] );
    void shuffle( Card * const );
    void deal( const Card * const );

    int main()
    {
    Card deck[ 52 ];
    const char *face[] = { "Ace", "Deuce", "Three",
    "Four", "Five",
    "Six", "Seven", "Eight",
    "Nine", "Ten",
    "Jack", "Queen", "King"};
    const char *suit[] = { "Hearts", "Diamonds",
    "Clubs", "Spades"};

    srand( time( NULL ) );

    fillDeck( deck, face, suit );
    shuffle( deck );
    deal( deck );
    return 0;
    }

    void fillDeck( Card * const wDeck, const char * wFace[],
    const char * wSuit[] )
    {
    int i;

    for ( i = 0; i <= 51; i++ ) {
    wDeck[ i ].face = wFace[ i % 13 ];
    wDeck[ i ].suit = wSuit[ i / 13 ];
    }
    }

    void shuffle( Card * const wDeck )
    {
    int i, j;
    Card temp;

    for ( i = 0; i <= 51; i++ ) {
    j = rand() % 52;
    temp = wDeck[ i ];
    wDeck[ i ] = wDeck[ j ];
    wDeck[ j ] = temp;
    }

    }


    switch (j){

    case 1;
    {
    num=1;
    }

    case 2;
    {
    num=2;
    }
    case 3;
    {
    num=3;
    }
    case 4;
    {
    num=4;
    }
    case 5;
    {
    num=5;
    }
    case 6;
    {
    num=6;
    }
    case 7;
    {
    num=7;
    }
    case 8;
    {
    num=8;
    }
    case 9;
    {
    num=9;
    }

    case 10;
    {
    num=10;
    }
    case "Queen";
    {
    num=10;
    }
    case "King";
    {
    num=10;
    }
    }

    void deal( const Card * const wDeck )
    {
    int i;
    char reply;

    for ( i = 0; i <= 0; i++ )
    printf( "%5s of %-8s%c", wDeck[ i ].face,
    wDeck[ i ].suit,
    ( i + 1 ) % 2 ? '\t' : '\n' );


    printf("Would You Like Another Card? " );
    scanf("%c",&reply);

    if(reply='Y')
    {
    for ( i = 0;i<=0; i++ )
    printf( "%5s of %-8s%c", wDeck[ i+1 ].face,
    wDeck[ i +1].suit,
    ( i + 1 ) % 2 ? '\t' : '\n' );
    if(num+num!=21)
    printf("Would You Like Another Card? " );
    scanf("%c",&reply);

    if(reply='Y')
    {
    for ( i = 0;i<=0; i++ )
    printf( "%5s of %-8s%c", wDeck[ i+1 ].face,
    wDeck[ i +1].suit,
    ( i + 1 ) % 2 ? '\t' : '\n' );
    }
    }
    }

    Its o.k, I'm sorry I'm lost I think I'm just gonna restart it, I was at a point where I had it running so it shuffled and dealt 2 random cards and asked if you wanted another one...my switch seemed to screw it up, I wanted a switch to store the values...I'm confused anyways so I'll restart, thanks for trying to help bhoys


  • Registered Users, Registered Users 2 Posts: 7,278 ✭✭✭kenmc


    Erm, where exactly is the switch??? it looks like it's located in the middle of no-where - i.e. there's an end brace after the shuffle function, and then there's the switch statement - sitting out on it's own.... needs to be in a function.
    K


  • Registered Users, Registered Users 2 Posts: 7,278 ✭✭✭kenmc


    oh and the error I get is this:
    test.c:65: error: parse error before "switch"
    /me uses gcc on cygwin for what it's worth


  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    either i'm an idiot or he's still doing case 1;


  • Closed Accounts Posts: 7,230 ✭✭✭scojones


    Also, you can't check in the switch for "Queen", only integers, and you need to delete the } before the switch statement, otherwise the switch statement is in limbo.


    Below code works fine. you need to re-think the "Queen" and "King" thing though
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    
    int num;
    struct card {
    const char *face;
    const char *suit;
    
    };
    
    typedef struct card Card;
    
    void fillDeck( Card * const, const char *[],
    const char *[] );
    void shuffle( Card * const );
    void deal( const Card * const );
    
    int main()
    {
    Card deck[ 52 ];
    const char *face[] = { "Ace", "Deuce", "Three",
    "Four", "Five",
    "Six", "Seven", "Eight",
    "Nine", "Ten",
    "Jack", "Queen", "King"};
    const char *suit[] = { "Hearts", "Diamonds",
    "Clubs", "Spades"};
    
    srand( time( NULL ) );
    
    fillDeck( deck, face, suit );
    shuffle( deck );
    deal( deck );
    return 0;
    }
    
    void fillDeck( Card * const wDeck, const char * wFace[],
    const char * wSuit[] )
    {
    int i;
    
    for ( i = 0; i <= 51; i++ ) {
    wDeck[ i ].face = wFace[ i % 13 ];
    wDeck[ i ].suit = wSuit[ i / 13 ];
    }
    }
    
    void shuffle( Card * const wDeck )
    {
        int i, j;
        Card temp;
    
         for ( i = 0; i <= 51; i++ ) {
           j = rand() % 52;
           temp = wDeck[ i ];
           wDeck[ i ] = wDeck[ j ];
           wDeck[ j ] = temp;
        }
    
    switch (j){
    
    case 1:
    {
    num=1;
    }
    
    case 2:
    {
    num=2;
    }
    case 3:
    {
    num=3;
    }
    case 4:
    {
    num=4;
    }
    case 5:
    {
    num=5;
    }
    case 6:
    {
    num=6;
    }
    case 7:
    {
    num=7;
    }
    case 8:
    {
    num=8;
    }
    case 9:
    {
    num=9;
    }
    
    case 10:
    {
    num=10;
    }
    /*case "Queen":
    {
    num=10;
    }
    case "King":
    {
    num=10;
    }*/
    }
    }
    void deal( const Card * const wDeck )
    {
    int i;
    char reply;
    
    for ( i = 0; i <= 0; i++ )
    printf( "%5s of %-8s%c", wDeck[ i ].face,
    wDeck[ i ].suit,
    ( i + 1 ) % 2 ? '\t' : '\n' );
    
    
    printf("Would You Like Another Card? " );
    scanf("%c",&reply);
    
    if(reply='Y')
    {
    for ( i = 0;i<=0; i++ )
    printf( "%5s of %-8s%c", wDeck[ i+1 ].face,
    wDeck[ i +1].suit,
    ( i + 1 ) % 2 ? '\t' : '\n' );
    if(num+num!=21)
    printf("Would You Like Another Card? " );
    scanf("%c",&reply);
    
    if(reply='Y')
    {
    for ( i = 0;i<=0; i++ )
    printf( "%5s of %-8s%c", wDeck[ i+1 ].face,
    wDeck[ i +1].suit,
    ( i + 1 ) % 2 ? '\t' : '\n' );
    }
    }
    }
    


  • Closed Accounts Posts: 579 ✭✭✭Magnolia_Fan


    Its sound everybody I've sorted it out...the problem was actually one misplaced } only it didn't show up on the error, thanks for the help


  • Registered Users, Registered Users 2 Posts: 1,481 ✭✭✭satchmo


    Its sound everybody I've sorted it out...the problem was actually one misplaced } only it didn't show up on the error, thanks for the help
    No less than three people already pointed out this problem... did you even read the replies on this thread?!


  • Closed Accounts Posts: 579 ✭✭✭Magnolia_Fan


    I did read the replys and it wasn't that it was wrong for some reason you know the yoke at the top that shows the functions...it was faded at that certain part so I just deleted it and put it in again and it worked


  • Registered Users, Registered Users 2 Posts: 7,278 ✭✭✭kenmc


    thats very scientific.... "the yoke at the top that shows the functions". i actually have no idea what you're talking about!!!!


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,562 ✭✭✭Snaga


    But his lecturer will think hes great now that his assignement works, so it doesnt matter if you can understand him or not ;)


Advertisement