I've written the following code. If I input 333 250 40 then using the formula requiredrate = (target-crun)/rball*6; the output should be 12.60 (I calculated with calculator). But the program shows -37.35.
Why?
Where is the problem?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
usingnamespace std;
int main ()
{
double orun,crun,rball,runrate,requiredrate;
double target=orun+1;
cin>>orun>>crun>>rball;
requiredrate = (target-crun)/rball*6;
cout<<requiredrate<<endl;
return 0;
}
On line 8 you set target=orun+1, but orun is not initialized. In C++, local variables are not initialized by default, so they contain whatever data happens to be on the stack at their assigned location.
I'm not sure if you need to initialize orun, or set target after line 10 when orun has the value that you input.