Need Help With My Loops

I cant for the life of me get my loops to work. One part of the loop will be fine, then another wont be fine, and no matter how many times I try to fix it, something goes wrong.

Now this program is meant to determine if a number 1 thru 1000 is a prime number, and if it isnt a prime, to display its divisors. The program is supposed to repeat each time you enter a number. I would also like it so that typing 0 exits the program.

I should also mention that my professor does not want us to use breaks or continues in the loops.

Any help would be greatly appreciated

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{
	int x = 0;
	int y;
	bool Prime = true;

	do
	{
		cout << "Enter a number 1 - 1000. ";
		cin >> x;
		while (!cin || (x < 0 || x>1000))
		{
			cout << "Invalid number. Try again." << endl;
			cout << "Enter a number 1 - 1000: ";
			cin.clear();
			cin.ignore(200, '\n');
			cin >> x;
		}
		if (x >= 1 || x <= 1000)
		{
			for (y = 2; y <= x / 2; ++y)
			{
				if (x%y == 0)
				{
					Prime = false;
					cout << "This is not a prime number." << endl;
					cout << "Its divisors are:" << endl;
					for (y = 1; y <= x; ++y)
					{
						if (x%y == 0)
						{
							cout << " " << y;
						}
					}
				}
				else
				{
					cout << "This is a prime number." << endl;
				}
			}
		}
		else
		{
			system("pause");
			return 0;
		}
		cout << endl;
	} while (x >= 1 || x <= 1000);
}
Last edited on
a) The program is supposed to repeat each time you enter a number
b) I would also like it so that typing 0 exits the program.
c) I should also mention that my professor does not want us to use breaks or continues in the loops.


You and your professor need some days off :-)
(Ok, it's just a joke)

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <limits>
#include <vector>

int main()
{
    int usernumber = 0;
    do
    {
        std::cout << "Please, enter a number between 1 and 1000 "
                     "or 0 to exit: ";
        std::cin >> usernumber;
        while(usernumber<0 || 1000<usernumber)
        {
            std::cout << "Invalid number. Try again." << std::endl;
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Please, enter a number between 1 and 1000 "
                         "or 0 to exit: ";
            std::cin >> usernumber;
        }

        if(usernumber != 0)
        {
            if(usernumber == 1)
            {
                std::cout << "This is a prime number." << std::endl;
            }
            else
            {
                std::vector<int> divisors;
                for(int x=2; x<=usernumber/2; x++)
                {
                    if(usernumber%x == 0)
                    {
                        divisors.push_back(x);
                    }
                }

                if(divisors.empty())
                {
                    std::cout << "This is a prime number." << std::endl;
                }
                else
                {
                    std::cout << "This is not a prime number and "
                              << "its divisors are:";
                    for (unsigned x=0; x<divisors.size(); x++)
                    {
                        std::cout << ' ' << divisors[x];
                    }
                    std::cout << std::endl;
                }
            }
        } // end if usernumber != 0
    
    } while(usernumber != 0);

    return 0;
}

Last edited on
I dont mean to sound ungrateful, but is there any way the program can be written without vectors? We havent covered that yet :(
Just use an ordinary array :+)
We havent done arrays yet either...

@_@
Starting from line 22 put that in a while loop and check each variable before asking for another so you dont need to place it into a vector or array. Both however are very simple (especially vectors) and you will most likely be covering it in class the first couple weeks
Does the variable check go in that line 22 loop, or is that a separate comment?
Yes, once the user enters the single number you want to go through to checks (the if statements) to see if it is prime and also give its divisors. Once that is done the program should loop unless the user enters 0. Also if you need to display all the divisors you can put that section in a while loop as well if youre not comfortable using arrays/vectors

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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
do
	{
		cout << "Enter a positive number 1 - 1000 "
				"or 0 to exit: ";
		cin >> x;
		while (!cin || (x < 0 || 1000 < x))
		{
			cout << "Invalid number. Try again." << endl;
			cout << "Enter a positive number 1 - 1000 "
					"or 0 to exit: ";
			cin.clear();
			cin.ignore(200, '\n');
			cin >> x;
		}
		if (x != 0)
		{
			for (y = 2; y <= x / 2; ++y)
			{
				if (x%y == 0)
				{
					Prime = false;
				}
			}
			if (Prime)
			{
				cout << x << " is a prime number." << endl;
			}
			else
			{
				cout << x << " is not a prime number." << endl;
				cout << "Its divisors are:" << endl;
				for (y = 1; y <= x / 1; ++y)
				{
					if (x%y == 0)
					{
						cout << " " << y;
					}
				}
				cout << endl;
			}
		}
		cout << endl;
	} while (x != 0);
	system("pause");
	return 0;


So using help from all of you this is what I came up with, and it seems to work. So thank you.

I was wondering if anyone could help me out with getting the divisor output to show no more than 5 numbers per line?
Giving such a short piece of code that doesn't even compile means pushing one's own luck.

getting the divisor output to show no more than 5 numbers per line?


No vectors, no arrays, no more than five divisors per line:
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
#include <limits>

int main()
{
    int usernumber = 0;
    do
    {
        std::cout << "\nPlease, enter a number between 1 and 1000 "
                     "or 0 to exit: ";
        std::cin >> usernumber;
        while(usernumber<0 || 1000<usernumber)
        {
            std::cout << "Invalid number. Try again." << std::endl;
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "\nPlease, enter a number between 1 and 1000 "
                         "or 0 to exit: ";
            std::cin >> usernumber;
        }

        if(usernumber != 0)
        {
            if(usernumber == 1)
            {
                std::cout << "This is a prime number." << std::endl;
            }
            else
            {
                int goes = 6;
                for(int divisors=2; divisors<=usernumber/2; divisors++)
                {
                    if(usernumber%divisors == 0)
                    {
                        if(goes == 6) {
                            std::cout << "This is not a prime number and "
                                      << "its first five divisors are:"
                                      << std::endl;
                            goes--;
                        }
                        if(goes > 0)
                        {
                            std::cout << divisors << ' ';
                            goes--;
                        }
                        if(goes == 0)
                        {
                            goes = 5;
                            std::cout << std::endl;
                        }
                    }
                }
                if(goes == 6){
                    std::cout << "This is a prime number." << std::endl;;
                }
            }
        } // end if usernumber != 0
    
    } while(usernumber != 0);

    return 0;
}

Topic archived. No new replies allowed.