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!

Recommended Answers

All 4 Replies

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.

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.

system("pause");

since your actual problem is solved, I'd like to suggest one more thing. using system("pause") to hold output on screen is a bad idea.
use

std::cin.get();  or directly cin.get(); //if you have declared using namespace std

which is more standard than a system call. you can have a look at this for more info. :)

http://www.dreamincode.net/forums/topic/30581-holding-the-execution-window-open/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.