0

Ok, so I got an error when I tried to print something based on the user's input. Pretty standard stuff, right? So, if the program would have worked correctly, the user would have entered six words or phrases that would be stored in the string named PhrasesAndWords. Then, each part of the array would have been tested, by creating a while-loop, using the counter as the index in the switch statement. Well, apparently, this didn't work, because it wasn't a constant expression, or a constexpr. The variable can't be a constant expression, though, since that would result in an infinite loop. By the way, here is the error:

C:\Users\henry\Desktop\NotTheActualPathForThisProject\main.cpp|34|error: switch quantity not an integer|

Aaand here's the code I wrote (I have gotten rid of irrelevant variables and such, though):

int main() {

string phrasesAndWords[6];

cin >> phrasesAndWords[0] >> phrasesAndWords[1] >> phrasesAndWords[2] >> phrasesAndWords[3] >> phrasesAndWords[4] >> phrasesAndWords[5]; // Recieve input

int counter = 0;

    while (counter < 6) {
    switch(phrasesAndWords[counter]) {

        case "RandomString":
            print("That sure was quite random. \n")
        default:
            print("I don't understaahnd... \n")
    };
    counter++;
};

};

Mo Abdul-Hameed
  • 6,030
  • 2
  • 23
  • 36

2 Answers2

1

Switch in C++ doesn`t work with strings. Cosider mapping the expected cases with integers.

Charles
  • 98
  • 8
0

The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.

start2learn
  • 23
  • 2
  • 7