Good day! I have this class for Players:

#include<iostream>
using namespace std;

class Players
{
      public:
              char Level[2];
              float SetPower();
};

Players Pl,

float Players::SetPower()
    {
        switch(Pl.Level)
            {
                case '1':
                return 5000; break;
                .........
            }
    }

int main()
{
    Pl.Level='1';
    cout<<"Your current Power is: "<<Pl.SetPower()<<endl;
    system("pause");
    return 0;
}

Now I got this error: "switch quantity not an integer". Im not switching "int" here but "char". Whats wrong with my code?
I dont want yo use if else as it makes it ugly.
Thank you!

Edited 4 Years Ago by blocker

You're using a char array (ie. a string), which the switch statement cannot work with. I tested it with switch(Pl.Level[0]), and it compiles. On the same note, Pl.Level='1'; would change the address of the pointer, not the first value in the array.

Edited 4 Years Ago by nmaillet

The expression of a switch statement must be of an integer type. That means you can use a single char but not a char array.

Thank you nmaillet, NathanOliver!

Converting the switch variable make it workes since I only need the first character.

This article has been dead for over six months. Start a new discussion instead.