+ 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; }
19 odpowiedzi
+ 9
floating point value is not allowed in switch multiply x by 10 and use case 11: and case 56
+ 8
ya you cant use floating point in switch
+ 7
try case 11 and 56 .. after multiplying x by 10
+ 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
+ 6
any time
+ 2
good one @kenAc
+ 1
float n
+ 1
Freeze I agree with you
+ 1
Freeze I agree with you
+ 1
nice explain thanks @zaid
+ 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;
}
+ 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;
}
0
if i do not use int n
0
float n ; // giving error :'(
0
well i want to call case 1.1 or 5.6 then what i did ?
0
11 and 56 is another option.
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 ?
0
thanks @kenAc
- 1
N has a type of integer, you switch-case loop checks for float type of N.