+ 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
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;
}
+ 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
+ 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.
+ 4
@ Sami @Dennis okay ...thank u soo much :))
+ 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..
+ 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
+ 3
you are coding in c++.. it won't work for string in switch case in c++ try using char or int
+ 3
@dennis that was useful .....thanks a lot :)
+ 3
@Ace ....yup ,I understood that :3....thanks a lot:)
+ 2
show your code please
+ 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;
}
}