#### Archived

This topic is now archived and is closed to further replies.

# beginner stuff...help!

This topic is 6489 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

hi. i''m quite new here. enjoy reading all the posts but i havent really learnt anything because it''s a bit too complex for me... i''ve only been programming for 2 weeks( or 2 2hour lecture), but am enjoying it. i have to do it for uni cs course you see... i have a bit of a problem with this code. the aim of the exercise is to find the prcentage of ''e'' characters in the whole sentance(not including the spaces). i can compile it and get it to run, but only if i declare one of the int(either non or e) as a float. it does not matter which i make a float, either one will do.(which is a bit strange). in this code, i have put both of them as a float:
#include
void main (void)
{
//int non, e;   this line does not work??? had to put both as float...
float total, e, non;
char letter;
e=0;
non=0;
cout << "please type in a sentance followed by a full stop" << endl;
do
{
cin >> letter;
if (letter==''e'' || letter==''E'') e++;
else non++;
}
while (letter != ''.'');
//cout << e << non << endl;  just part of a testing line to check for errors
total = 100 * (e / (e+non));
cout << total << "%";
}

any help would be appreciated greatly...

##### Share on other sites
What you do when you divide an int by an int is called integer division, which means you only take the integer part of the result.

Example:

2/3 = 0
4/3 = 1
21/6 = 6

When you convert on of the opendants to a float you no longer do an integer division but a floating point division, in other words you keep the part after the decimal point too.

##### Share on other sites
what happens when you dont use floats (int e,non)??

is the result wrong or is there an error message from the compiler??

i think its a wrong result, and when it is, then its just a problem of the integer:
total = 100 * (e / (e+non));
this calculates a division of a nonfloatingpoint trought a nonfloatingpoint, for example 3/2.. but both are nonfloating => result is saved nonlfoating, and that cant be 1.5, it has to be 1, or 2 (think it is 1), and then a multiplication *100 results 100 instead of 150.. just as an example

for calculateing with floating points, even when you use integers, you have to say it to the compiler:
total=100.0f*((float)e/(float(e+non));

like this..
when you use vc++, you dont need to write 100.0f, it automatically use it as a float, at other compilers i dont know

just try it

we wanna play, not watch the pictures

##### Share on other sites
A small change in the order of operations will probably solve the problem while still using integers:

total = (100*e)/(e + non);

That should be accurate to within 1 percentage point, but it''ll always round down.

##### Share on other sites
thanks everyone.
i just assumed that making the answer a float would be alright...
i think i need to learn a bit more on int devision and everything like that.

##### Share on other sites
quote:

float1 = int1 / int2;
should work, atleast it works for me

That''s odd, because it shouldn''t. What the compiler *should* do in the case above is calculating int1/int2 (returning an int), and then placing that value in float1.
If you need to do int/int and get a float you should typecast one of the int''s.

float1 = int1 / (float) int2;

(It doesn''t matter which int you typecast, as long as you cast at least one.)

-Neophyte

- Death awaits you all with nasty, big, pointy teeth. -

##### Share on other sites
You could also write

total = 100 * (e / (e+(float)non));

and declare e and non as ints.

Here the compiler converts non to a float and continues using float divides.

But intvar = (float)2/(float)3 will return 0 or 1 (I think 1) and
not 0.66, because the compiler converts the float value back to an int and stores it into intvar.

Hope that helps, too, ...
Coder10000

1. 1
Rutin
24
2. 2
3. 3
JoeJ
20
4. 4
5. 5

• 9
• 46
• 41
• 23
• 13
• ### Forum Statistics

• Total Topics
631749
• Total Posts
3002033
×