Divisor Help

I have to write a program for a class that reads an unknown number of positive integers and for each number, uses a nested counter-controlled while loop to print its divisors and a count of the divisors.

I seem to be having trouble... my file input has three numbers, 1, 4, and 1680. My output only shows "4 * 1 = 4"

Here are my loops...


fin >> inval;

while (fin >> inval)
{
total = total + 1;

while (inval >= divisor)
{

if ((inval % divisor) == 0)
{
divtotal = divtotal + 1;
fout << inval << " = " << divisor << " * " << inval/divisor << endl
<< inval << " has " << divtotal << " divisors" << endl;
return 0;
}
}
}

fout << "There were " << total << " numbers processed" << endl;
code tags + indentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fin >> inval;

while (fin >> inval)
{
   total = total + 1;

   while (inval >= divisor)
   {

      if ((inval % divisor) == 0)
      {
         divtotal = divtotal + 1;
         fout << inval << " = " << divisor << " * " << inval/divisor << endl
              << inval << " has " << divtotal << " divisors" << endl;
         return 0;
      }
   }
}

So, line 1 reads a number (1) and stores it in inval. Line 3 does the same (thus inval = 4). Then while condition succeeds, since 4 > 1. Then you try 4%1 which, of course, is 0, thus stuff is printed. Then you reach return 0; and the program ends.

Note that this code would not work as divisor is neither reset for each number, nor ever incremented.
How would I reset and increment the divisor for each number? (sorry, I'm a n00b) :-)
Analyze the algorithm of finding the divisors of one number (n) first.
The point is to try each number (i) and see if n is divisible by it. i starts from 1 (you could start from 2 (unless that would require copying some code), since you know that every number is divisible by itself). After you try 1, you have to make i = 2, etc. (actually this would be a good place for a for loop, but that is not necessary).
After you have that part working, just put it in a while(fin>>n) loop.
Topic archived. No new replies allowed.