Pages: [1] 2   Go Down
Author Topic: Working with Case & Switches  (Read 1026 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

* inverse with stages.txt (11.07 KB - downloaded 23 times.)
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 524
Posts: 26420
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.

Offline Offline
Edison Member
*
Karma: 32
Posts: 1382
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't forget default: !!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't forget default: !!

I don't know what you mean.
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

'switch' works with integer values only

Code:
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;
}
« Last Edit: April 04, 2013, 11:03:03 pm by lloyddean » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 524
Posts: 26420
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

@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.
Logged

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.

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 524
Posts: 26420
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 524
Posts: 26420
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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.

Offline Offline
Faraday Member
**
Karma: 59
Posts: 2811
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Faraday Member
**
Karma: 59
Posts: 2811
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Des Moines, WA - USA
Offline Offline
God Member
*****
Karma: 25
Posts: 779
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 13
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1] 2   Go Up
Jump to: