Stuck on a switch with a char

I'm getting a compile error with the following:

char Code1[4];
   
    sprintf(Code1, "%sss", "365");
   
       switch (Code1)
    {
    case '365':
      Serial.println("Yes 365 Days in a Year!");
//      playcomplete("01.WAV");
      break;
    case '2':
//      playcomplete("02.WAV");
      break;
    }

Error: switch quantitly not an integer.

I'm able to do this

  char key = keypad.getKey();
  
  if(key)  // same as if(key != NO_KEY)
  { 
    switch (key)
    {
    case '1':
      playcomplete("01.WAV");
      break;
    case '2':
      playcomplete("02.WAV");
      break;
    }
 }

In this case key is a char and it works with the switch. I'm missing something - Any pointers.
What am I missing on the first approach? Thank you

a char is 8 bits of memory, or one ASCII (or extended ASCII) letter. is "365" one letter?

switch statements only work on one number. You'll have to expand it and use strcmp to see if the strings' values are the same.

if (strcmp(Code1, "365")) {}
else if (strcmp(Code1, "2")) {}

There are other ways to do this, but this is probably a good start.

char Code1[4];
   
  sprintf(Code1, "%s", "365");
   
  if ( (Code1[0] == '3') && (Code1[1] == '6') && (Code1[2] == '5') )
    Serial.println("Yes 365 Days in a Year!");

Each element is treated separately above.
You can also convert a string to integer using atoi and then use its output in your switch, eg:

char Code1[4];
int i;

  sprintf(Code1, "%s", "365");

  i = atoi(Code1);
  switch (i)
  {
   case 365:
       Serial.println("Yes 365 Days in a Year!");
       break:
  }

Hope that helps.

edit: found a bug over a year later.

What you missed is that an array of anything (even an array of ints) is not an int. When you use a char in place of an int, the compiler will generally give you the benefit of the doubt and 'promote' the char to an int (but be wary of this).

sprintf(Code1, "%sss", "365");

This is going to write 365ss and a trailing NULL (that's 6 values) into your 4 element array, and trash something else.

The format specifier for a string is %s, regardless of the number of characters in the string.

The good news is the error is gone and my code is running with the above suggested changes.

BUT I have questions on the responses.

1 - The following is from Arduino's References
Parameters

var: the variable whose value to compare to the various cases

label: a value to compare the variable to
...
So I'm uncertain about the comment that switch statements only work on one number. I took it from the reference that the label is a value to compare. Any clarification? Is the switch label limited to on number?

2 - j514 Thank you the iatoi solution worked, but I'm wondering if I'm going to get myself into trouble based on the comment that a switch only works with one number. Also is there a function to convert a int to a char?

3 - Morris thank you that clears what I thought I saw but wasn't sure. 'the complier will generally give you the benefit of the doubt and 'promote' the char to an int'. I'll keep that in mind.

4 - PaulS thank you for the clarification on the 'Format Specifier'.

Is Code1 always going to be a string representation of an integer? If so, use atoi().

char Code1[4];
sprintf(Code1, "%sss", "365");
int code = atoi(Code1)

switch (code) ...

Edit: Removed code I meant to delete before clicking post.

Clarification - from ISO/IEC 9899:TC3 (The C Language Standard):

6.8.4.2 The switch statement
Constraints
1 The controlling expression of a switch statement shall have integer type.
2 If a switch statement has an associated case or default label within the scope of an identifier with a variably modified type, the entire switch statement shall be within the scope of that identifier.135)
3 The expression of each case label shall be an integer constant expression and no two of the case constant expressions in the same switch statement shall have the same value after conversion. There may be at most one default label in a switch statement. (Any enclosed switch statement may have a default label or case constant expressions with values that duplicate case constant expressions in the enclosing switch statement.)
Semantics
4 A switch statement causes control to jump to, into, or past the statement that is the switch body, depending on the value of a controlling expression, and on the presence of a default label and the values of any case labels on or in the switch body. A case or default label is accessible only within the closest enclosing switch statement.
5 The integer promotions are performed on the controlling expression. The constant expression in each case label is converted to the promoted type of the controlling expression. If a converted value matches that of the promoted controlling expression, control jumps to the statement following the matched case label. Otherwise, if there is a default label, control jumps to the labeled statement. If no converted case constant expression matches and there is no default label, no part of the switch body is executed.

I'm wondering if I'm going to get myself into trouble based on the comment that a switch only works with one number

I'm not sure what you mean. Switch() can compare against an unlimited (?) amount of numbers, and also has a default in the case that none of them matched, eg:

   switch (i)
   {
    case 6:
    case 12:
       Serial.println("I got 6 or 12");
       break:

    case 0:
    case -1:
    case -3:
       Serial.println("0, -1 or -3");
       break:

    case 365:
       Serial.println("I got 365");
       break:

    default:
       Serial.println("Nothing matched..");
       break:
   }

edit: note to self: don't reply to forum posts at 4am.

Morris thank you for the reference - would be handy if the ISO/IEC 9899:TC3 (The C Language Standard) was referenced in the Arduino Ref.

J514 - Yea that is what I thought - multiple length number Okay. I asked because WizenedEE mentioned (first reply in post) 'switch statements only work on one number.' I took that to mean single length numbers and wasn't sure because switch will work with multiple length numbers as you pointed out in your code.

Thank you for everyones reply.

The standard document is available as a free download in final draft form at www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf. It's the ultimate reference for standard C, and I've found it convenient to keep a copy on my machine.

One of the C++ folks can probably share a link to a an official C++ standard.