#include<iostream.h>
void main()
{
start:
int m1,m2,m3,o1,o2,o3,con;
float per;
char name[40];
cout<<"\n Enter name : ";
cin>>name;
cout<<"\n Enter marks in sub.1 : ";
cin>>m1;
cout<<"\n Out of : ";
cin>>o1;
cout<<"\n Enter marks in sub.2 : ";
cin>>m2;
cout<<"\n Out of : ";
cin>>o2;
cout<<"\n Enter marks in sub.3 : ";
cin>>m3;
cout<<"\n Out of : ";
cin>>o3;
per=((m1+m2+m3)/(o1+o2+o3))*100;
cout<<"\n Hi "<<name<<" You have got "<<per<<" % mark";
cout<<"\n Do you wish to continue? 1/2 : ";
cin>>con;
if (con<2) goto start;
cout<<endl;
}
mail me my problem........
[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
[ about comp.lang.c++.moderated. First time posters: do this! ]
> per=((m1+m2+m3)/(o1+o2+o3))*100;
m1+m2+m3 results in an int, and so does o1+o2+o3. The division operator has
therefore two int operands and will perform an integer division, returning
an int (in the process losing the fractional part of the division). The
multiplication now also has two int operands an will return an int. Finally
the assignment operator will convert the int to a float.
per=(static_cast<float>(m1+m2+m3)/(o1+o2+o3))*100
m1+m2+m3 is now converted to a float, so that the division operator has a
float and an int operand and will perform a float division, returning a
float. The multiplication now also has a float and an int operand and will
return a float.
Berend
as m1, m2, m3, o1, o2, o3 are all ints and normally o1+o2+o3 > m1+m2+m3
the result of the division (integer) is zero. 0*100 is still zero and
converted to a float it becomes 0.0
--
Francis Glassborow
Check out the ACCU Spring Conference 2002
4 Days, 4 tracks, 4+ languages, World class speakers
For details see: http://www.accu.org/events/public/accu0204.htm
--
Maciej Sinilo (Yarpen/Substance)
http://www.rde.prv.pl
You are performing an integer division, so any fractions are dropped.
If you want to perform float division, either the nominator or
denominator
has to be of type float. You can cast one of them to float to fix that.
Yours sincerely,
bjorn
This is an integer division, since m1,m2,m3,o1,o2 and o3 are all
integers.
The conversion to float takes place after the division.
> per=((m1+m2+m3)/(o1+o2+o3))*100;
> cout<<"\n Hi "<<name<<" You have got "<<per<<" % mark";
> cout<<"\n Do you wish to continue? 1/2 : ";
> cin>>con;
> if (con<2) goto start;
> cout<<endl;
> }
[ Send an empty e-mail to c++-...@netlab.cs.rpi.edu for info ]
Integer division is the problem, as others answered.
But making either operand float isn't the only possible solution;
in this particular case where what you ultimately get is an integer
by multiplying 100, you could also do like this:
per = 100 * (m1+m2+m3) / (o1+o2+o3);
By multiplying first, you don't lose all your significant digits
below the decimal point, even with integer division.
This has another advantage that you avoid floating-point arithmetics,
which mattered a lot in the past for performance reasons (but I'm not
sure whether it does these days also).
Note, however, that an overflow is possible if (m1+m2+m3) is large.
(No problem in most cases. ;-) )
--
Sungbom Kim <musi...@bawi.org>