+ 3

How can I input string value ?

in cpp when I wrote : string shape; cin>>shape; switch (shape ) error say that switch quantity not integer value ....what dose that mean ? dose that mean that the way for cin strings value is wrong? or what

20th Jul 2017, 8:21 PM
Zainab
Zainab - avatar
12 Réponses
+ 5
this is the code I asked for #include <iostream> #include<string> using namespace std; int main(){ string shape ; cin>> shape ; switch (shape){ case 's' : int a,b ,c; cout <<"enter parameters of sqaure, please "<<endl; cin>>a; b = 4*a; cout <<"the circumference is "<<b<<endl; c = a*a; cout <<"the area is "<<c<<endl; break ; } return 0; }
20th Jul 2017, 8:34 PM
Zainab
Zainab - avatar
+ 4
@ Sami Khan ...I tried to input char instead of string and my cases was case 'a' , case 'b' and so on ...it worked truly
20th Jul 2017, 8:29 PM
Zainab
Zainab - avatar
+ 4
The problem is not your switch, the problem is the switch. C++ switch does not accept a string, just integral types like: int, long long, unsigned, char, bool. Or anything else that can be represented as an integral like an enum or a class type contextually implicitly convertible to an integral or enumeration type.
20th Jul 2017, 8:43 PM
Dennis
Dennis - avatar
+ 4
@ Sami @Dennis okay ...thank u soo much :))
20th Jul 2017, 8:45 PM
Zainab
Zainab - avatar
+ 3
how your code.. the problem is in your switch case. u might be passing case 1 case 2 case 3..and so on.. so 1,2,3 these are integers, so it won't work..
20th Jul 2017, 8:26 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 3
it didn't work when I use "s" but when I use char instead of string (but not use the name "shape" use any Char) it worked truly 😅 so that I thought the problem with string I thought that we need some process to input the string like java
20th Jul 2017, 8:42 PM
Zainab
Zainab - avatar
+ 3
you are coding in c++.. it won't work for string in switch case in c++ try using char or int
20th Jul 2017, 8:42 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 3
@dennis that was useful .....thanks a lot :)
20th Jul 2017, 8:51 PM
Zainab
Zainab - avatar
+ 3
@Ace ....yup ,I understood that :3....thanks a lot:)
21st Jul 2017, 8:11 AM
Zainab
Zainab - avatar
+ 2
show your code please
20th Jul 2017, 8:31 PM
Мг. Кнап🌠
Мг. Кнап🌠 - avatar
+ 2
Btw Here's what I mean with "class type contextually implicitly convertible to an integral": #include <iostream> class MyClass { public: MyClass(int n):x(n){} operator int() // <-- { return x; } private: int x; }; int main() { MyClass c(1); switch(c) { case 1: std::cout << ":)" << std::endl; break; case 2: std::cout << ":(" << std::endl; break; } }
20th Jul 2017, 8:47 PM
Dennis
Dennis - avatar