+ 1

why is this not working?

I keep getting compilation error #include <iostream> using namespace std; int main() { int o; int i; int in; float R; cout << "Calculating Shunt Resistance (Rsh)"<<endl ; cin >> o >>endl ; cin >> a >>endl ; cin >> in >>endl ; R = (a /(in -a))×o; cout << "Shunt Resistance= "<< R << endl; return 0; }

8th Jul 2016, 8:32 PM
Darion Clarke
Darion Clarke - avatar
10 Respuestas
+ 1
R = (float) (i / (in - i)) * o; It is returning an int result so you have to cast to float. Also put a return statement at the end.
8th Jul 2016, 11:27 PM
Tony Christopher
Tony Christopher - avatar
+ 1
The int doesn't have a decimal part (the mantissa) so 1/2 would become 0 instead of 0.5. On my compiler by casting to float the mantissa part remains from an equation using int. The compiler was probably warning you that you would lose the mantissa.
9th Jul 2016, 3:06 AM
Tony Christopher
Tony Christopher - avatar
0
#include <iostream> using namespace std; int main() { int o; int i; int in; float R; cout<<"Calculating Shunt Resistance (Rsh)"<<endl; cin>>o; cin>>i; cin>>in; R=(i/(in-i))*o; cout<<"Shunt Resistance= "<<R<<endl; }
8th Jul 2016, 9:13 PM
Nikola Pesic
Nikola Pesic - avatar
0
still not working
8th Jul 2016, 10:59 PM
Darion Clarke
Darion Clarke - avatar
0
Sorry I didn't see your return.
8th Jul 2016, 11:29 PM
Tony Christopher
Tony Christopher - avatar
0
Also you have to check (in - i) != 0, or you'll get a division by zero error.
8th Jul 2016, 11:49 PM
Tony Christopher
Tony Christopher - avatar
0
till is not work
9th Jul 2016, 2:14 AM
Himanshu vaishnav
Himanshu vaishnav - avatar
0
ok guys i got it to work... aparently i needed to declare all variables as a float... can someone explain y
9th Jul 2016, 2:41 AM
Darion Clarke
Darion Clarke - avatar
0
cause u r entering float value.
9th Jul 2016, 6:32 AM
Prashant
Prashant - avatar
0
no.. all the values that i entered were integers but my result is a float value. i want to know y my inputs need to be floats to get a float result
9th Jul 2016, 7:33 AM
Darion Clarke
Darion Clarke - avatar