Program not running correct but no errors

Hello, I am new to this site and new to C++. I failed a homework assignment because my program is not working properly. I dont think there is any reason for me to try and move on to other assignments until i figure out what the problem is. I dont really need exact answers but maybe if someone could point me into the right direction that would be most helpful.

These are my instructions.

Write a program that generates all the factors of a number entered by the user. For instance, the number 12 has the factors 2 * 2 * 3. This program has the following requirements:

A. The user must enter a positive integer. If the user enters something else, your program should output an error message and let the user enter a new value. Use a do/while loop to make sure the user input is successful.

B. The factors must be output in increasing order. The lowest factor your program should report is 2.

C. Your program should output 4 factors per line, each factor in a field of 10 characters. (Hint: the number of factors output determines when to output endl!)

On my program if i enter 12 as my input my answer is 5 and 5 which is wrong.

Here is my program, any comments, help, or just pointed in the right direction would be great. I did look around the forums before posting and found similar problems but nothing helped me out so thanks in advance to any help..

#include <iostream>
#include <iomanip>
using namespace std;

int main (void)
{
int numberIn;

do
{
cout <<"Enter a positive integer to be factored: " << flush;
cin >> numberIn;
if (numberIn <= 0)
{
cout <<" you did not enter a positive number." <<endl;
}
}while (numberIn <= 0);
int factorcount = 0,
factor = 2;
while (numberIn > 1)
{
if (numberIn % factor)
{
cout << setw(10) << factor;
numberIn /= factor;
if (++factorcount == 4)
{
cout << endl;
factorcount = 0;
}
}
else
{
++factor;
}
}
cout << "Press any key to exit." << endl;
cin.ignore(2)
return 0;
}
I think the problem is on this line if (numberIn % factor)
if (numberIn % factor)

The above statement will be true if numberIn % factor is non-zero. That is, the statement would be true if factor is not a factor of numberIn. This is the opposite of what you intended. Though a failure is a tad harsh for this, in my opinion.
Last edited on
ah i think i see! If ( numberIn % factor ==0 )
Im not sure if that is even correct but it outputs correct answers.
Last edited on
peter87 and shacktar thanks a ton for showing me where i was having trouble, i tried all my test cases and it works great now. Thanks alot!
Topic archived. No new replies allowed.