Problem with "While" loop

Hi, I recently started coding in C++ and came to the subject of loops and the keyword while. I tried to create a simple program that finds the prime factors of a number. So I came up with this...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main ()
{
    using namespace std;
    
    cout << "Please enter in a number to find the prime factors: " << endl;
    int nPrime;
    cin >> nPrime;
      
    int nCount = 1;
    
         while (nPrime % nCount == 0)
        {
            cout << nCount << ", ";
            nCount++;
        }
    
}

This is the closest I could get to it doing what I have asked it to do which is find the prime factors of the number entered however with this one the best it does is, from what I have seen, give me the numbers one and two then terminate. From what I know I thought that the keyword while made the terms inside of it repeat until the condition set was no longer true. Please put me in the right direction with this...
1
2
3
4
while(condition)
{
//code
}


The code portion will only execute while condition is true. Say the user enters 14, when the code reaches the loop (12 % 1 == 0) is true, so the code enters the loop, then at the end, tests it again, with nCount being 2 now, so (12 % 2 == 0) is also true, then it tries it again, with nCount being 3, (14 % 3 == 0 ) is false, so now you do not reenter the loop, you are done with it, and because that is the last line of the code, the program ends. What you probably want to do is repeat the loop while nCount is smaller then nPrime, and use an if statement to see if nPrime % nCount == 0 is true from within the loop, and if it is, then display that nCount.
Oh okay. I think I understand the problem a bit better now and came up with a new code based on what you said however this code produces a very weird error that I really don't see what the problem is. Could you please try to help me with this,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

int main ()
{
    using namespace std;
    
    cout << "Please enter in a number to find the prime factors: " << endl;
    int nPrime;
    cin >> nPrime;
    
         while (nPrime > 0)
        {
            int nCount = 1;
            if (nPrime % nCount == 0)
            {
                cout << nCount << ", ";
            }
            
            nCount++;
        }
    
}


It produces a constant stream of ones that continue until the user terminates the program. Could you please take another look?
look up your condition in while loop again.
while (nPrime > 0)
Do you change nPrime inside loop? nope. That's why it cant exit from loop
Thanks a bunch guys :) You really helped me figure out what to o for the code. I'm going to post it here just in case anyone tries to do the same thing and runs across that problem so this is my final version of the code.

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

int main ()
{
    using namespace std;
    
    cout << "Please enter in a number to find the prime factors: " << endl;
    int nPrime;
    cin >> nPrime;
      
    int nCount = 1;
         while (nCount <= nPrime)
        {
            if (nPrime % nCount == 0)
            {
                cout << nCount << ", ";
            }
            
            nCount++;
        }
    
}
Topic archived. No new replies allowed.