Help with Programming!

Hello I am trying to get this dumbbell program to print out the factors of an integer in descending order. However many times I have tried to do different coding with if statements and other methods I keep on getting errors. If I could get help coding this problem that would be great! Please help!
Problem says:
Prompt the user for an integer. Print out the factors of the integer in descending order.

Example:
Input:
25

Output:
25
5
1

And here is my code up to this point
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
26
#include <iostream>
#include <string>
using namespace std;

int main(){
{
    int num = 0;
    cout << "Please put in a number" << endl;
    
    int value; 
    while (int n =value != -1)
    {
    cin >> value;


    Ask user for a number -> store it in num

        Loop through numbers starting at num, decrementing
        Use the modulo operator (%) to find the factors 
        if num % i is equal to 0, i is a factor
        Print i

    }   
    return 0;
}
 
Hello jollyholly11,

Line 10 value is uninitialized and contains garbage. So on line 11 you compare n, which equals zero, to value which contains garbage. Will not work the way you want. And the "int n" I am not sure if it will work there. The second part "!= -1" what is "!= -1"? I think what you might want is while (int n == value && something != -1) or while (int n == value || something != -1).

Line 13 should be above line 11 so that "value" will have a value for line 11.

Lines 16 - 21 needs some actual code to work with.

Write some code and post the response and you will receive a better response.

Hope that helps,

Andy

EDIT: disregard the last two sentences of the first paragraph. Now that I understand better what you are doing with the condition of the while loop thay really do not apply.
Last edited on
Hello jollyholly11,

After writing the program and then rewriting the program to use a while loop I did com to understand what you were trying to do with line 11 with the while loop. Although some of what I said about the while still applies disregard the last part where I mentioned a different to write the while condition.

Line 11 in the while condition int n = value mostly pointless since "n" and "value" are not needed or used in the program. It would make more sense to write while (num != -1). And based on what you say on line 16 line 13 should be cin >> num; not cin >> value.

Lines 18 - 21 tells you what to do. All you need to do is put that into actual code.

Hope that helps,

Andy
Topic archived. No new replies allowed.