Go Down

Topic: Working with Case & Switches (Read 1 time) previous topic - next topic

chipthomas

I have been working with this code for sometime and would like to turn it into a multi stage program.  Once a person finds the place the box is suspose to be, I want to make it change to a different set of cords and have them to their and then change to another set of cords and so on.

Now I have started with the first switch and case #.  It seems to complile just fine.  But I am unsure as to where to place the "break" command.  I did place it at where I wanted to break and it seems to compile.  But when I start to place the "case 2:", I don't seem to be placing it in the correct location.

With it in the current location I get, "SlimJim_Inverse_Stages_and_switches:340: error: expected unqualified-id before 'case'".

I have tried searching for info, but I get more searchers about C++ than anything else and I can't really make out the differences.

Does anyone have any suggestions?

Thanks
Chip

CrossRoads

Code: [Select]

switch(varName){
case 1:
//
break;
case 2:
//
break;
case 'A':
//
break;
} // end switch:case

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

KeithRB


chipthomas


Code: [Select]

switch(varName){
case 1:
//
break;
case 2:
//
break;
case 'A':
//
break;
} // end switch:case




This I think I understand.  I just the placement of it inside my code.


lloyddean

#5
Apr 05, 2013, 05:57 am Last Edit: Apr 05, 2013, 06:03 am by lloyddean Reason: 1
'switch' works with integer values only

Code: [Select]

switch ( random('a', 'z') )
{
    case 'a':
        ... some useful code here ...
        break;

    case 'b':
        ... some useful code here ...
        break;

    case 'c':
        ... some useful code here ...
        break;

    case 'd':
        ... some useful code here ...
        break;

    case 'd':
    case 'e':
    case 'f':
    case 'q':
        ... some useful code here ...
        break;

    default:
        // handle all other values ...
        break;
}


CrossRoads

@lloyddean, what are you saying?

"In particular, a switch statement compares the value of a variable to the values specified in case statements. When a case statement is found whose value matches that of the variable, the code in that case statement is run. "

The examples show case 1, case 2, and I know case 'a', case 'b' work also.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

chipthomas

Is a "default" case required?  Is that why I am getting the error?


@lloyddean, what are you saying?

"In particular, a switch statement compares the value of a variable to the values specified in case statements. When a case statement is found whose value matches that of the variable, the code in that case statement is run. "

The examples show case 1, case 2, and I know case 'a', case 'b' work also.

CrossRoads

No, a default case is not required. If none of the cases are met, the code just continues on after the end  }.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

chipthomas


No, a default case is not required. If none of the cases are met, the code just continues on after the end  }.


Can you tell if I am placing it correct in my code?

CrossRoads

I only see a Case 1, it looks good to start.
Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

michinyon

The compiler error message you are quoting,   should state which line of the program the error is in.    Well,  which line is it ??

michinyon

Even if the compiler accepts it,    coding a switch statement with   1,2,'A', ...   is simply unwise programming.

It is difficult to figure out what it will do.   Unless you are trying to be deliberately obscure.

lloyddean

Either show the whole sketch or more code above and below the 'switch' statement.

I was showing the use of 'default' as well as multiple 'case' statements dropping into the same code.

'case' statements need not be in any particular order either.

chipthomas

No I'm not trying to be deliberately obscure.  I'm sorry if it came across like that.  It would be just my lack of know how.  Another suggestion was to put things into different functions because of the massive amount of code that i already have.  Then use the switch cases to call those functions.

What do you suggest using with the switch statements other than 1,2 and so on.  I am working with a multi stage Geocache and was going to use stage 1 then stage 2 and so on.  I so far have been able to have the "stages" change like I want them to.  Or so I think I do.   


Even if the compiler accepts it,    coding a switch statement with   1,2,'A', ...   is simply unwise programming.

It is difficult to figure out what it will do.   Unless you are trying to be deliberately obscure.



Thank you for looking at the code.  I removed the Case 2 because of the error and started with that.  I try building the functions and try calling them from there.  I just have to figure out how in incorporate some of the code from the void setup into the void loop/function call. 

I only see a Case 1, it looks good to start.

Go Up