Hello ladies and gents,

Ive written a program wich I'm almost certain it'll work, Ive only got one problem, I need to write an if like this:

int a = 0;


if ( u!=x)
a++;

Now, u is a floating variable wich is divided before this selection by 2, 3, 5.

It's got to state that when u equals any floating point like 1.1 untoo 1.9 it shouldn't ad one to a, if u is 2.0 or 3.0 or whatever aslong as it's not .1 untill 0.9, it should ad a++

It's probably something simple wich I'm not seeing, but I can't figure out what :)

Any help would be appreciated.

Recommended Answers

All 9 Replies

Could you post the full code and btw im not sure as to EXACTLY what you are asking here....

What is x? What is u? What is the significance of a? as a increases its not affecting x or u....

Ok, here's the code Ive written so far:

int main()
{
	int a=0, b=0, c=0, x, number;
	
	cout<< "Give ten numbers and see how many are dividable by 2, how many by 3 and how many by 5! "<<endl;

	for (int i=0; i<4; i++)
	{
		cin>> x;cin.get();

		float result= float(x)/2;
		if (result1== number)
			a++;
			float result2=float(x)/3;
			if (result2== number)
				b++;
				float result3=float (x)/5;
				if (result3!= number)
					c++;
	}

		cout<< "The amount dividable by 2 are: "<< a <<endl;
		cout<< "The amount dividable by 3 are: "<< b <<endl;
		cout<< "The amount dividable by 5 are: "<< c <<endl;

	cin.get();

	return 0;
}

if (result1== number)// this is the problem here, if result is equal to anything but .0, a shouldn't be added by one, like if result equals 1.8 or 5.3 a should remain as before, only if it would be 1.0 or 5.0, a should be added by one!

Hope you understand what I mean.

Thanks for the help ;)

Have you not tried using the % operator. It gives the remainder of division.

int number = 244; // test number

if((number % 2) == 0)
{
    // number is divisible by 2
}
else if((number % 3) == 0)
{
    // number is divisible by 3
}
else if((number % 5) == 0)
{
    // number is divisible by 5
}

btw number is zero as you havent initialised it so you are testing the fraction against 0, which is GOING to fail as it wont show you if it divides cleanly

Thanks for the help, seems you we're thinking about modulo as some of my friends we're :D

int main()
{
	int a=0, b=0, c=0, x, getal=0;
	
	cout<< "Lees een rij van tien positieve getallen in en bepaal hoeveel van deze getallen"
	" door 2, hoeveel door 3 en hoeveel door 5 deelbaar zijn! "<<endl;

	for (int i=0; i<9; i++)
	{
		cin>> x;cin.get();

		if (x%2 == getal)
			a++;
			if (x%3 == getal)
				b++;
				if (x%5== getal)
					c++;
	}

		cout<< "Het aantal gedeeld door 2 is gelijk aan: "<< a <<endl;
		cout<< "Het aantal gedeeld door 3 is gelijk aan: "<< b <<endl;
		cout<< "Het aantal gedeeld door 5 is gelijk aan: "<< c <<endl;

	cin.get();

	return 0;
}

Thanks again for the help ;)

nice1! im curious, what language is that you have in the code

"Lees een rij van tien positieve getallen in en bepaal..."?

cout<< "Lees een rij van tien positieve getallen in en bepaal hoeveel van deze getallen"
	" door 2, hoeveel door 3 en hoeveel door 5 deelbaar zijn! "<<endl;

Anything with that many "van" in it is bound to be Dutch!!!!!
If you are not Dutch, you are not much!
A quote from my Dutch friends.

@ 1o0oBhP

I'm from the Flemish part of Belgium, but we do speak the same language as the Dutch ;)

So, Dutch and Flemish writing are one and the same :!:

Nice! as you can guess i am from the UK.... :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.