Factor Calculator Displaying Numbers It Shouldn't

Hi, I am new to this forum, and I have some code in which I cannot understand what is going on.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <windows.h>

using namespace std;
int main()
{
    int testingNumber = 0;
    int dividingNumber = 1;
    int remainder = 0;
    cout << "Enter the number you would like to find the factors of." << endl;
    cin >> testingNumber;
    for(dividingNumber = 1; dividingNumber <= testingNumber; dividingNumber++);
    {
        cout << dividingNumber;
        system("pause");
        remainder = testingNumber % dividingNumber;
        if(remainder == 0);
        {
            cout << dividingNumber << ", ";      
        }
    }
    cout << endl << endl;
    system("Pause");
    return 0;
}

Line 14 is used for debugging, because if I enter 50 for the testingNumber, Line 14 always returns 51. If I enter 51 instead for the Testing Number, it shows 52. In other words, for some reason, even though I am defining dividingNumber = 1 in the line before, it always returns one above the testingNumber. I am expecting it to cout as "1" however. The program should do a loop and find the factors of the testingNumber, but it doesn't. Any ideas would be appreciated.

Thanks,
umop aplsdn.
You've got a semi-colon at the end of line 12.
Okay, I have fixed that, and I have removed line 14 and line 15 as those were for debugging. This is what it looks like now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <windows.h>

using namespace std;
int main()
{
    int testingNumber = 0;
    int dividingNumber = 1;
    int remainder = 0;
    cout << "Enter the number you would like to find the factors of." << endl;
    cin >> testingNumber;
    for(dividingNumber = 1; dividingNumber <= testingNumber; dividingNumber++)
    {
        system("pause");
        remainder = testingNumber % dividingNumber;
        if(remainder == 0);
        {
            cout << dividingNumber << ", ";      
        }
    }
    cout << endl << endl;
    system("Pause");
    return 0;
}


Now when I enter 50, the result is:

1
2
3
4
5
6
7
Enter the number you would like to find the factors of.
50
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 2
3, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 4
3, 44, 45, 46, 47, 48, 49, 50,

Press any key to continue . . .

It doesn't check to see if it is a factor of 50. I think this is because I have a semicolon after the ) in if. I am guessing that you cannot put ;'s on the ends of if's and for's and while's and etc? If that is true, I am about to facepalm.

EDIT: Tested it. EUREKA, it works! I have come to the conclusion that you must not put ;'s after things like if or while after the parenthesis.

The final code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <windows.h>

using namespace std;
int main()
{
    int testingNumber = 0;
    int dividingNumber = 1;
    int remainder = 0;
    cout << "Enter the number you would like to find the factors of." << endl;
    cin >> testingNumber;
    system("cls");
    cout << "The factors of " << testingNumber << " are: " << endl << endl;
    for(dividingNumber = 1; dividingNumber <= testingNumber; dividingNumber++)
    {
        remainder = testingNumber % dividingNumber;
        if(remainder == 0)
        {
            cout << dividingNumber << ", ";      
        }
    }
    cout << endl << endl;
    system("Pause");
    return 0;
}

If anyone wants it (which I doubt). I'm going to expand on it to make it a Perfect Number calculator/lister, and a prime calculator/lister.
Last edited on
Topic archived. No new replies allowed.