Question

Howdy guys,

so i am trying to use Case operation on a String ,is Case operators only works with Integers ? Because the compiling error :

switch quantity not an integer

And i have this code as an example :

#include <iostream>
using namespace std;

 int main(){
string type;
cout<<"Please enter your computer type (PC ,Laptop, Smartphone) : "<<endl;
getline(cin, type);

 switch(type){

case "PC":
 cout<<"Ok so you'vr got a "<<type;
 break;

case "Laptop":
 cout<<"Aha ,u got a "<<type;
 break;

case "Smartphone":
 cout<<"That s not bad ,u ve got :"<<type;
 break;
}


return 0;
}

I know that i can use other logical operators like IF ; but i am stydying the Case operators so i am curious can i work with Strings or not.

Was it helpful?

Solution

The c/c++ switch statement only operates on numeric integer primitives.

You cannot use it with char* pointers (literals).

attr(optional) switch ( condition ) statement

condition - any expression of integral or enumeration type, or of a class type with a conversion function to integral or enumeration type, or a declaration of a single non-array variable of such type with a brace-or-equals initializer.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top