0

I have 2 errors while trying to put a variable in switch case:

#include <stdio.h>
int main(int argc, char *argv[]) {
  float conversie = 0;
  float leu = 1;
  float usd = 3.6 * leu;
  float eur = 4.4 * leu;
  float aur = 139 * leu;
  float suma;
  float valoare;
  char tipmoneda;
  printf("introdu moneda pe care vrei sa o schimbi:");
  scanf("%c\n", &tipmoneda);
  switch (tipmoneda) {
  case 'usd':

  default:
    break;
  }

}

These are the errors:

Untitled.c:14:8: warning: multi-character character constant [-Wmultichar] case 'usd': ^
Untitled.c:14:8: warning: overflow converting case value to switch condition type (7697252 to 100) [-Wswitch]
2 warnings generated.
Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
  • these are the errors: – Alex Sturza Sep 05 '14 at 17:42
  • Untitled.c:14:8: warning: multi-character character constant [-Wmultichar] case 'usd': ^ Untitled.c:14:8: warning: overflow converting case value to switch condition type (7697252 to 100) [-Wswitch] 2 warnings generated. – Alex Sturza Sep 05 '14 at 17:43
  • probably because you are trying to switch on a char array, not a single char – Ryan Sep 05 '14 at 17:45
  • no,i want to switch on the variables eur,usd,aur – Alex Sturza Sep 05 '14 at 17:46
  • 1
    Change to `char tipmoneda[10]; scanf("%9s", tipmoneda); if (strcmp(tipmoneda, "usd") == 0) ... else if (strcmp(tipmoneda, "eur") == 0) ..."` – chux - Reinstate Monica Sep 05 '14 at 17:48
  • those are warnings, not errors. it is because you are using single quotes to hold multiple characters. http://stackoverflow.com/questions/3683602/single-quotes-vs-double-quotes-in-c – Colin D Sep 05 '14 at 17:49
  • possible duplicate of [C/C++: switch for non-integers](http://stackoverflow.com/questions/4165131/c-c-switch-for-non-integers) – Colin D Sep 05 '14 at 17:52
  • @chux: `'usd'` is legal regardless of `sizeof (int)`; its value is implementation-defined. But yes, it's almost certainly not what anyone wants. – Keith Thompson Sep 05 '14 at 18:20
  • @Ani: `usd` would be an identifier. `'usd'` is a character constant; it has an implementation-defined value of type `int`. All unprefixed character constants are of type `int` (in C; in C++ they're of type `char`). – Keith Thompson Sep 05 '14 at 18:22
  • @Keith Thompson Agreed: LSNED "The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined. C11dr §6.4.4.4 10 – chux - Reinstate Monica Sep 05 '14 at 18:25

4 Answers4

2

If you are entering "usd" on the input line, this won't work; scanf will only read and store the leading 'u' character into tipmoneda (which won't match the multi-character constant 'usd'). Also, to be safe, you should put a leading blank space in the scanf control string:

scanf( " %c", &tipmoneda );

This will tell scanf to skip over any leading whitespace.

What you can do is simply enter 'u' for usd, 'e' for eur, 'a' for aur, and then switch as follows:

switch( tipmodena )
{
  case 'u' : 
    // process for usd
    break;

  case 'e':
    // process for eur
    break;

  case 'a':
    // process for aur
    break;

  default:
    // unrecognized option
    break;
}

Edit

If you really want to enter "usd" instead of "u", you could do something like the following:

#include <ctype.h>   // needed for the tolower call below
...
char tipmoneda[4] = {0}; // read tipmoneda as a 3-character string instead of
                         // a single char
...
scanf( "%3s", tipmoneda ); // note conversion specifier
switch( tolower( tipmoneda[0] )) // switch on lowercase form of first letter
{
  // same cases as above
}

The tolower call will convert the argument to lower case, so you could enter "usd", "USD", "Usd", etc., and still have this work. Note that if you enter a string that's more than 3 characters long, the remaining characters will remain in the input stream, potentially fouling up the next scanf call.

John Bode
  • 119,563
  • 19
  • 122
  • 198
1

'usd' is a multi character integer constant (with portability problems), yet tipmodena is simply 1 char. A new approach is needed.

The below forms the case constants at compile time from 3 char. At run-time the string read forms the integer to switch on in the same fashion.

[Edit]
Reworked hash function to cope with signed char, overflow shifting of signed char and potential 16-bit int.

#include <stdio.h>
#include <string.h>

#define HASH(a,b,c) \
    ((unsigned char)(a)*0x10000u + (unsigned char)(b)*0x100u + (unsigned char)(c))

int main(void) {
  char denom[4] = { 0 };
  while (scanf("%3s", denom) == 1) {
    switch (HASH(denom[0],denom[1],denom[2])) {
      case HASH('u', 's', 'd'):
        puts("usd");
        break;
      case HASH('e', 'u', 'r'):
        puts("eur");
        break;
      case HASH('a', 'u', 'r'):
        puts("aur");
        break;
      default:
        printf("Unknown '%s'\n", denom);
    }
    memset(denom, 0, sizeof denom);
  }
  return 0;
}
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
1

C switch case only works with integer or single character types.

srsci
  • 239
  • 5
  • 10
0

This is simply part of the language definition. You may only use constant expressions for your case values. Examples include numeric and character literals as well as previously-defined constants.

Technically, 'usd' is a multi-character constant, which is interpreted as a numeric value. See Multiple characters in a character constant for reference. This is generally not an advisable practice for portable code.

This also appears to conflict with the inferred intentions of the poster. It appears that the poster is attempting to read a currency as a string and calculate totals based on that input. Currently, this code will read a single character and switch on that value.

Community
  • 1
  • 1
Amish Programmer
  • 2,051
  • 3
  • 19
  • 22