-
strange calculation
Hai,
I am using Borland 3.0 DOS and windows XP. Well am expecting my program result 0.2 , on the contrary my program gives 0.00 as result. What is wrong with my prog.??? it looks fine for me.
Thanks
Code:
#include<iostream.h>
#include<iomanip.h>
#include<constream.h>
#include<stdlib.h>
#include<dos.h>
#include<conio.h>
#include<stdio.h>
void main()
{
clrscr();
double n;
constream cout;
n = 2+5 - 4*3 / 10 %2 -6;
cout <<setxy(1,1) <<setiosflags(ios::showpoint) <<setw(10)
<< setprecision(3) <<n ;
getch();
}
-
> I am using Borland 3.0 DOS and windows XP
You should really upgrade your compiler
> void main()
Yeah right
Use int main
As endless posts / FAQs will tell you
> n = 2+5 - 4*3 / 10 %2 -6;
Its an integer expression, so everything truncates to 0
Say perhaps
n = 2.0+5.0 - 4.0*3 / 10 %2 -6.0;
-
hmm
4*3=12
12/10=1.2
1.2%2=0 R 1.2
2+5=7
7-1.2=5.8
5.8-6=-.2
hmm
curious
Couldn't find the operator precedence for % but I am assuming it's L-R and the same as * (multiplcation) and division.
I suggest using () to make the equation crystal clear.
-
try using float instead of double perhaps?
-
constream.h?
the header constream.h isn't included with Visual Studio or Borland 5.02 :(
You really should upgrade your compiler so other people can help you.
-
I would suggest using float and use braces to clearly write out your equation. It might also be easier if you perform the calculations in different parts.
float a,b,c;
a = 3.0*5.5;
b = 2.2/1.1;
c = a/b;
cout<<c<<endl;
-
> 4*3=12
> 12/10=1.2
Actually, its 12/10 = 1
It's all integer arithmetic at this point - its only at the assignment that the whole thing gets converted to a double.