error: expected unqualified-id before 'switch

Hi folks. I'm getting errors with the following construct. Not sure what the problem is. Essentially it's to choose which account to post under, as I have more than 1 account.

switch(account)  {
  case 'alpha'
    Twitter twitter("alpha:alphapassword");
    char msg[] = "Alpha Twitter post";
    break;
  case 'bravo'  {
    Twitter twitter("bravo:bravopassword");
    char msg[] = "Bravo Twitter post";
    break;
    }

I'm not familiar with C code, so maybe I'm overlooking something. Any ideas?

Cheers

Single quotes are for characters. Double quotes are for strings of characters. You can't use strings in case statements.

I see. OK, the latter is probably what I'm tripping on. Thanks for the info.

Your post title says “error: expected unqualified-id before 'switch”, and yet you didn’t post any code before the switch statement, where the error might be seen.

The quotations were throwing the error as I was instantiating a variable above:

char account = "dingbat";

But it isn't a character, but a string. Why won't that work?

"dingbat" isn't a character. It's an array of characters. You need to declare the variable as an array.

char account[] = "dingbat";

or

char *account = "doofus";

OK, got past that part. How do you integrate that array into an if statement?

if(account == "doofus") {
   // statements
} else if(account == "dingbat") {
 // statements
}
if(strcmp (account,"doofus") == 0)

Ah thank you sir. Still getting the error that says there’s something happening before. Here is the full code:

#include <Ethernet.h>
#include <Twitter.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 9 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };
char account[] = "dingbat"; 

if(strcmp(account, "dingbat") == 0) {
  Twitter twitter("alpha:alphapassword");
  char msg[] = "alpha alpha alpha";
} else if (strcmp(account, "wobbly") == 0)  {
  Twitter twitter("bravo:bravopassword");
  char msg[] = "bravo bravo bravo";
}

...

I don't think you posted the "full code".

Executable code (i.e. stuff that isn't just declarations) like that has to be in a function, like "loop" or "setup", and ellipses are not part of Wiring.

#include <Ethernet.h>
#include <Twitter.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 9 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };
char account[] = "dingbat";

if(strcmp(account, "dingbat") == 0) {
  Twitter twitter("alpha:alphapassword");
  char msg[] = "alpha alpha alpha";
} else if (strcmp(account, "wobbly") == 0)  {
  Twitter twitter("bravo:bravopassword");
  char msg[] = "bravo bravo bravo";
}

void setup()  {
  
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(9600);
  
  delay(1000);
  
  Serial.println("connecting ...");
  if (twitter.post(msg)) {
    int status = twitter.wait();
    if (status == 200) {
      Serial.println(msg);
      Serial.println("OK!");
    } else {
      Serial.print("failed : code ");
      Serial.println(status);
    }
  } else {
    Serial.println("connection failed.");
  }
}

void loop()
{
}

Executable code (i.e. stuff that isn't just declarations) like that has to be in a function, like "loop" or "setup".

I'd use the preprocessor, since these are effectively static assignments.

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 1, 9 };
byte gateway[] = { 192, 168, 1, 1 };
byte subnet[] = { 255, 255, 255, 0 };

#define ACCNT_DINGBAT 0
#define ACCNT_WOBBLY  1

#define THIS_ACCNT ACCNT_DINGBAT

#if THIS_ACCNT == ACCNT_DINGBAT
char account[] = "dingbat";
Twitter twitter("alpha:alphapassword");
char msg[] = "alpha alpha alpha";
#endif

#if THIS_ACCNT == ACCNT_WOBBLY
char account[] = "wobbly";
Twitter twitter("bravo:bravopassword");
char msg[] = "bravo bravo bravo";
#endif

In function ‘void setup()’:
error: ‘twitter’ was not declared in this scope

Points to the second if statement. This is the example twitter post, which works:

#include <Ethernet.h>
#include <Twitter.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 10, 0, 0, 177 };
byte gateway[] = { 10, 0, 0, 1 };
byte subnet[] = { 255, 255, 0, 0 };

Twitter twitter("YourID:Password");
char msg[] = "Hello, World! I'm Arduino!";

void setup()
{
  Ethernet.begin(mac, ip, gateway, subnet);
  Serial.begin(9600);
  
  delay(1000);
  
  Serial.println("connecting ...");
  if (twitter.post(msg)) {
    int status = twitter.wait();
    if (status == 200) {
      Serial.println("OK.");
    } else {
      Serial.print("failed : code ");
      Serial.println(status);
    }
  } else {
    Serial.println("connection failed.");
  }
}

void loop()
{
}

You'll note that in that example there is no executable code outside of "setup" and "loop".

What executable code was outside those methods in the last example I posted?

The whole of the "strcmp" conditional.

My last post showed the conditional inside the setup(). Anyway, too much hassle. The language I am used to allows for easier use of conditionals for me to create a testing environment.

Thanks for your help.

My last post showed the conditional inside the setup().

Your last post showed the conditional inside "setup", and your comment was that it was an example which worked.

This is the example twitter post, which works:

So I wasn't commenting on that, because you said it worked.

I was commenting on this:

if(strcmp(account, "dingbat") == 0) {
  Twitter twitter("alpha:alphapassword");
  char msg[] = "alpha alpha alpha";
} else if (strcmp(account, "wobbly") == 0)  {
  Twitter twitter("bravo:bravopassword");
  char msg[] = "bravo bravo bravo";
}

void setup()  {

Where the illegal conditional is clearly before "setup".

This is where I assumed you wanted it to be, which is why I posted the preprocessor solution.

There are obviously other solutions.

I posted the conditional inside the setup, which does not work either. The error says that Twitter does not work in that scope...something similar. I am guessing that the whole conditional is not registering somehow.

Anyway, no worries.

I am guessing that the whole conditional is not registering somehow.

You're guessing wrong.