+ 1

Error in program . please remove error

#include <iostream> using namespace std; int main() { int n; cin>>n; switch(n){ case 1.1:cout<<n*1.1;break; case 5.6:cout<<n*5.6;break; default:cout<<n; } return 0; }

9th Oct 2017, 5:15 PM
Anuj kumar
Anuj kumar - avatar
19 Antworten
+ 9
floating point value is not allowed in switch multiply x by 10 and use case 11: and case 56
9th Oct 2017, 5:24 PM
zaid
zaid - avatar
+ 8
ya you cant use floating point in switch
9th Oct 2017, 5:38 PM
zaid
zaid - avatar
+ 7
try case 11 and 56 .. after multiplying x by 10
9th Oct 2017, 5:39 PM
zaid
zaid - avatar
+ 7
you cant use point in switch statement ... to solve this problem we multiply the number by 10 or 100 or 1000 depending upon the number of digits after the point eg: cin<<n; n*=10; switch(n){ case 11: ...;break; case 56:....;break; i am not that good at explaining stuff .. hope your doubt is clear nw
9th Oct 2017, 5:48 PM
zaid
zaid - avatar
+ 6
any time
9th Oct 2017, 5:52 PM
zaid
zaid - avatar
+ 2
good one @kenAc
11th Oct 2017, 9:53 AM
Ngwemo Ongori
Ngwemo Ongori - avatar
+ 1
float n
9th Oct 2017, 5:31 PM
Anuj kumar
Anuj kumar - avatar
+ 1
Freeze I agree with you
9th Oct 2017, 5:31 PM
Mouhamed Diouf
Mouhamed Diouf - avatar
+ 1
Freeze I agree with you
9th Oct 2017, 5:32 PM
Mouhamed Diouf
Mouhamed Diouf - avatar
+ 1
nice explain thanks @zaid
9th Oct 2017, 5:51 PM
Anuj kumar
Anuj kumar - avatar
+ 1
More detailed solution: #include <iostream> #include <cmath> // needed for round using namespace std; int main(){ float n; int i_n; //needed for switch case cin >> n; i_n= round(n*10); // Testoutput cout<<"n= "<<n<<"\ni_n= "<< i_n<<endl; switch(i_n){ case 11: cout << n*1.1; break; case 56: cout << n*5.6; break; default: cout << n; } return 0; }
11th Oct 2017, 5:20 AM
KenAc
KenAc - avatar
+ 1
//Shortversion of upper code #include <iostream> #include <cmath> // needed for round using namespace std; int main(){ float n; cin >> n; switch((int)round(n*10)){ case 11: cout << n*1.1; break; case 56: cout << n*5.6; break; default: cout << n; } return 0; }
11th Oct 2017, 5:35 AM
KenAc
KenAc - avatar
0
if i do not use int n
9th Oct 2017, 5:30 PM
Anuj kumar
Anuj kumar - avatar
0
float n ; // giving error :'(
9th Oct 2017, 5:37 PM
Anuj kumar
Anuj kumar - avatar
0
well i want to call case 1.1 or 5.6 then what i did ?
9th Oct 2017, 5:41 PM
Anuj kumar
Anuj kumar - avatar
0
11 and 56 is another option.
9th Oct 2017, 5:42 PM
Anuj kumar
Anuj kumar - avatar
0
float cant be used in switch as you said okay then i replaced 1.1 and 5.7 to 11 and 56 respectively . if i want to call case 11 how ?
9th Oct 2017, 5:57 PM
Anuj kumar
Anuj kumar - avatar
0
thanks @kenAc
11th Oct 2017, 11:07 AM
Anuj kumar
Anuj kumar - avatar
- 1
N has a type of integer, you switch-case loop checks for float type of N.
9th Oct 2017, 5:20 PM
Freezemage
Freezemage - avatar