Hi everyone,
I spent 2 hours trying to make this code work but I couldn't do it.
Can someone point out what I'm missing or what I'm doing wrong?
Thanks in advance.

#include<iostream>
using namespace std;

bool isFactor(int a,int b); 

int main()
{

	int num1, num2;

	cout<<"Enter a number: ";
    cin>>num1;
	cout<<"The factors of "<<num1<<" are: ";
   	for(num2=2; num2=1; num2++);
	{
		 if (isFactor (num1%2==0));
		    cout<<num2<<" ";
			num2=2;
}
     cout<<endl;    
	return 0;
}

bool isfactor(int a, int b)
{
	if(a/b)
		return true;
	else
		return false;
}

This problem is about printing all the factors of any integer on the screen.
Yes is homework and I really tried doing it (you can see some errors) but as I said it's not working for me. Some guidance would be great.

Recommended Answers

All 3 Replies

Your function isFactor expects two arguments, but you only pass it one.

if ( isFactor (num1%2==0) );

The trailing semicolon is likely extraneous and problematic as well.

This is an assignment, not a comparison. And again the semicolon ends the loop before the body begins.

for ( num2=2; num2=1; num2++ );

Also, C++ is case sensitive, so isFactor is not the same as isfactor .

[edit]I think you want a loop like this:

for ( num2 = 2; num1 > 1; num2++ )
   {
      if ( isFactor (num1, num2) )
      {
         cout << num2 << " ";
         num1 /= num2;
      }
   }

And your function to be:

bool isFactor(int a, int b)
{
   return a % b == 0;
}

Dave, thanks for your help, your code works and I decided to use your changes in my old code and they work as well.
But now the program is not terminating, any ideas?

Thanks again,
Alfonso

Post your updated code.

[edit]Ah, yes. The for loop should probably not always increment num2 .

for ( num2 = 2; num1 > 1;  )
   {
      if ( isFactor (num1, num2) )
      {
         cout << num2 << " ";
         num1 /= num2;
      }
      else
      {
         ++num2;
      }
   }
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.