Trouble using GOTO with the SWITCH statement!

This is a discussion on Trouble using GOTO with the SWITCH statement! within the C Programming forums, part of the General Programming Boards category; For some reason it wont compile and I get the error messages (in the goto line) below Linetest.c:40: error: expected ...

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    4

    Trouble using GOTO with the SWITCH statement!

    For some reason it wont compile and I get the error messages (in the goto line) below

    Linetest.c:40: error: expected identifier or ‘*’ before ‘case’
    Linetest.c:44: error: expected identifier or ‘*’ before ‘case’
    Linetest.c:48: error: expected identifier or ‘*’ before ‘case’

    Ive got no idea why it wont compile :s Any help please, thanks!

    Code:
    #include <stdio.h>
    
    int numval;
    
    
    int main()
    {
    
    
    numval = 4;
    
    printf("\n Numval = %d \n", numval);
    
    
    
    
    switch (numval)
    {
    
    case 0 : {printf("\n Case 0 = %d \n\n", numval);} break;
    
    case 4 : {
    
    printf("\n Case 4 = %d \n\n", numval);
    
    } break;
    
    case 8: {
    
    printf("\n Case 8 = %d \n\n", numval);
    
    } break;
    
    case 12: {
    
    printf("\n Case 12 = %d \n", numval);
    printf("\n Both Motors Forward \n");
     if (numval != 4)
    {
    goto case 8;
    }
     if (numval != 8)
    {
    goto case 4;
    }
    if (numval = 12)
    {
    goto case 12;
    }
    
    } break;
    
    default: {printf("\n Default = %d \n\n", numval);} break;
    
    }
    
    
    
    return 0;
    
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,738
    I suggest that you turn that global variable into a local variable and format your code so that it is easier to read:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int numval = 4;
    
        printf("\n Numval = %d \n", numval);
    
        switch (numval)
        {
        case 0:
            {
                printf("\n Case 0 = %d \n\n", numval);
            }
            break;
        case 4:
            {
                printf("\n Case 4 = %d \n\n", numval);
            }
            break;
        case 8:
            {
                printf("\n Case 8 = %d \n\n", numval);
            }
            break;
        case 12:
            {
                printf("\n Case 12 = %d \n", numval);
                printf("\n Both Motors Forward \n");
    
                if (numval != 4)
                {
                    goto case 8;
                }
    
                if (numval != 8)
                {
                    goto case 4;
                }
    
                if (numval = 12)
                {
                    goto case 12;
                }
            }
            break;
        default:
            {
                printf("\n Default = %d \n\n", numval);
            }
            break;
        }
    
        return 0;
    }
    Now, I am not entirely sure what is the logic you are trying to implement, and I am not entirely sure if you can use goto on a case label in a switch, but you probably do not need to use goto here, and in fact you should avoid the use of goto.

    So, what exactly are you trying to do?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    Ive googled it and apparently you can use goto on a case label like Ive done but for some reason mine doesnt work :\ I know its not good to use goto but it seems the easiest way to keep things simple. Im programming some hardware in uni and seeing as im not allowed to bring it home i have to write chunks and plug in values and print statements instead of actually writing the whole program

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It looks he wants to loop while numVal is 12:

    Code:
    #include <stdio.h>
    
    int main()
    {
        int numval = 4;
    
        printf("\n Numval = %d \n", numval);
    
        do {
            switch (numval)
            {
            case 0:
                {
                    printf("\n Case 0 = %d \n\n", numval);
                }
                break;
            case 4:
                {
                    printf("\n Case 4 = %d \n\n", numval);
                }
                break;
            case 8:
                {
                    printf("\n Case 8 = %d \n\n", numval);
                }
                break;
            case 12:
                {
                    printf("\n Case 12 = %d \n", numval);
                    printf("\n Both Motors Forward \n");
                }
                break;
            default:
                {
                    printf("\n Default = %d \n\n", numval);
                }
                break;
            }
        } while (numval == 12);
    
        return 0;
    }
    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).

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    Thanks anon but thats not what im trying to do, as i will have similar if, goto statements in case 4 and 8 aswel so i dont want it to loop like that. I am open to suggestions with using goto as it might not be the best way to do it.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,738
    So what are you trying to do?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Mar 2010
    Posts
    4
    Its really hard to explain.. Im programming a buggy to follow a white line on the floor, it has two sensors, left sensor on line is 4, right sensor on line is 8, both on line is 12 and neither on line is 0. Ive made a flowchart and started to write this code but I cant get any further without the goto working or at least something very similar to goto.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Skip all the "on line" stuff. What do your sensors need to read, and what happens when they read something? Nothing about frequencies or whatever ... put it into words. What happens with the sensors find something or don't?


    Quzah.
    Hope is the first step on the road to disappointment.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,738
    Quote Originally Posted by xxJaRxx
    Its really hard to explain.. Im programming a buggy to follow a white line on the floor, it has two sensors, left sensor on line is 4, right sensor on line is 8, both on line is 12 and neither on line is 0. Ive made a flowchart and started to write this code but I cant get any further without the goto working or at least something very similar to goto.
    The trouble is, if you cannot explain it, you probably do not really know what you are doing. In fact, I know of programmers who explain what they are trying to do to the nearest unsuspecting victim (who may or may not be programmer) in order to better understand what they themselves are trying to do. Oh, and of course if you do not know what you want to do, we cannot really help you either.

    Nontheless, I shall give it a try. I suspect that you want to do action X when the left sensor is online, action Y when the right sensor is online, and action Z when both are online. But when both are online, you still want to do actions X and Y. If so, an easy solution is to place actions X and Y (and possibly also Z) in functions, then call these functions depending on what sensors are online (i.e., your switch will come into play).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while true
        if left and right
            do a thing
        else
        if left and not right
            do this
        else
        if right and not left
            do that
        else
            do another
    I'm not really sure why you need goto at all.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting the switch statement to work.
    By mtymightymike in forum C Programming
    Replies: 7
    Last Post: 10-15-2008, 06:32 PM
  2. Stack operations from switch statement elements
    By mlsrar in forum C Programming
    Replies: 15
    Last Post: 10-02-2008, 01:12 PM
  3. switch statement
    By guillermoh in forum C Programming
    Replies: 5
    Last Post: 03-10-2008, 02:17 PM
  4. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 03:05 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 03:49 PM

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