Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

percentage programm problem

0 views
Skip to first unread message

parag

unread,
Feb 9, 2002, 10:45:10 AM2/9/02
to
hi,

i have created a programm for finding the percentage in 3 sub. but when i run
the programm,per. comes out to be 0.
programm is as follows:

#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! ]

Berend Meijer

unread,
Feb 9, 2002, 2:39:01 PM2/9/02
to
> int m1,m2,m3,o1,o2,o3,con;
> float per;

> 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

Francis Glassborow

unread,
Feb 9, 2002, 8:54:17 PM2/9/02
to
In article <735f3ce6.02020...@posting.google.com>, parag
<para...@hotmail.com> writes

> cout<<"\n Out of : ";
> cin>>o3;
> per=((m1+m2+m3)/(o1+o2+o3))*100;

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

unread,
Feb 10, 2002, 4:35:22 AM2/10/02
to
In article <735f3ce6.02020...@posting.google.com>,
para...@hotmail.com (parag) says...

> hi,
>
> i have created a programm for finding the percentage in 3 sub. but when i
run
> the programm,per. comes out to be 0.
The bug is in the following line:
> per=((m1+m2+m3)/(o1+o2+o3))*100;
It performs integer division, which always results in zero (but for one
case - it would be one for 100%), then multiplies it by 100 (still zero)
and _then_ converts to floating-point (0.0f).
Solution? Force int->float conversion before division:
per=(float(m1+m2+m3)/(o1+o2+o3))*100;

--
Maciej Sinilo (Yarpen/Substance)
http://www.rde.prv.pl

bjorn rohde jensen

unread,
Feb 10, 2002, 4:39:20 AM2/10/02
to
Hi Parag,

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 ]

Sungbom Kim

unread,
Feb 11, 2002, 2:21:30 PM2/11/02
to
parag wrote:
>
> per=((m1+m2+m3)/(o1+o2+o3))*100;

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>

0 new messages