Char and switch statement issues

Hello, I've just started learning C++, and I can't figure out the issue with my code. I've tried a few different ways to get a switch to work with a single declaration, and for each case to assign different values to a char, but I keep getting incompatible types in assignment of 'const char' to 'char'. How am I using these two things wrong? I've already gone through a few posts on here, but I can't find anything that helps.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
char my_Command[];
char my_Gender_Name[10];
char my_Class_Name[10];
char my_History_Name[10];

int main()
    while (my_Command == 0){
        cin >> my_Command;
        switch(my_Command){
            case 'm': my_Gender_Name = "Male"; break;
            case 'f': my_Gender_Name = "Female"; break;
            case 's': my_Class_Name = "Soldier"; break;
            case 'w': my_Class_Name = "Wizard"; break;
            case 'a': my_Class_Name = "Assassin"; break;
            case 'b': my_History_Name = "Priest"; break;
            case 'c': my_History_Name = "Cook"; break;
            case 'g': my_History_Name = "Guard"; break;
            case 1: my_Complete = 1; break;
        }
    }


Any comments would be appreciated, thanks!
char my_Command[];

my_Command is a pointer to a char. I suspect you actually wanted it to be a char

char my_Command;


This:
1
2
3
4
5
6
7
8
 case 'm': my_Gender_Name = "Male"; break;
            case 'f': my_Gender_Name = "Female"; break;
            case 's': my_Class_Name = "Soldier"; break;
            case 'w': my_Class_Name = "Wizard"; break;
            case 'a': my_Class_Name = "Assassin"; break;
            case 'b': my_History_Name = "Priest"; break;
            case 'c': my_History_Name = "Cook"; break;
            case 'g': my_History_Name = "Guard"; break;

will all not work as you have incompatible types (e.g. "Guard" is a char[6] but you're trying to assign it to a char[10]). This is not how to copy an array of char.

You're making it much harder on yourself by using arrays of char. Don't use arrays of char. Use proper C++ string objects.
Last edited on
Topic archived. No new replies allowed.