Using the switch function in C ++ [duplicate]

2

The function switch and I do not know what I'm doing wrong I want to put the name marcos ai appear "marks and legal" and put murilo "murilo e bacana". I'm doing this:

#include <stdio.h>
#include <conio.h>
int main(void)
 {
  char marcos;
  printf("Digite um nome e veja? ");
  scanf("%s",&nome);
  switch (nome) 
  { 
    case 'marcos' : 
    printf("Marcos e legal");
    break;
    case 'murilo':
    printf("Murilo e bacana");
    break;
    default: 
    printf("errado");

     getch();
    return 0;
}

  }  
    
asked by anonymous 10.09.2018 / 00:54

1 answer

3

You can not use strings in case , there you can only use simple types like numbers and individual characters (which are still numbers). If you need to do with string you have to use if and strcmp() anyway. In C ++ you can use == in a type string of it.

You can understand How does the switch work under the covers? .

And if you were to use string it would have to be double quotation marks ( " ). Single quotation mark ( ' ) is for single character that is still a numeric type, but can be printed as a character. The string is just a sequence of these characters ending with a null (% with%) .

In addition, the variable nome was not declared, it was another nonsense that was not used. And it should be declared as array or make dynamic allocation that is very advanced.

And never use conio.h .

    
10.09.2018 / 00:59