Variable not acting as expected?

I am trying to learn C++ and currently constructing a program which will list the divisors of a number that is entered. I am not worried about loops or making the code "optimal" but just on how it all works etc.

I have got so far with trying to change a boolean to true based on another variable, but it never seems to change. The final COUT always returns a 0:

Please enter a whole number: 2
you entered 2
The remainder when divided by 2 is: 0
This number is even: 0 <-- should be True / 1!

Please can someone advise?

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
#include <iostream>
using namespace std;
int main()
{
    int number = 0; //Input Variable
    //Output test variables
    int test2 = 0;
    //Test Indicator Variables
    bool isDivBy2 = false;
    //Get input from console
    cout << "Please enter a whole number: ";
    cin >> number;
    while(!cin)
    {
        cout << "That was not a number! Please enter a whole number:  ";
        cin.clear();
        cin.ignore();
        cin >> number;
    }
    cout << "you entered " << number << endl;
    //Test for divisibility by 2
    test2 = number;
    test2 = test2 % 2; //modulo so determines remainder
    cout << "The remainder when divided by 2 is: " << test2 << endl;
    if (test2 = 0){
            isDivBy2 = true;
            return 2;
    }
    cout << "This number is even:     " << isDivBy2 << endl;
}
Look at line 25. What do you think test2 = 0 is doing?
Ah, = is assignment, == is comparison :) now to work out why the COUT at 29 doesn't always fire.

Thanks for your help.
Ah, = is assignment, == is comparison :)

Yep = a common mistake :)

now to work out why the COUT at 29 doesn't always fire.

Because your main function returns, ending the program, before it even reaches that line.
Last edited on
oh, the return 2;

I thought that would break me out of the if "loop" but looks like that was a red herring. Thanks again.

markscottuk wrote:
oh, the return 2;

I thought that would break me out of the if "loop" but looks like that was a red herring. Thanks again.


No - return immediately causes the function to return. And when the main function returns, the program terminates.

To exit a loop, use break. Although an if statement isn't a loop.

Thanks again.

You're welcome :)
Last edited on
This code now works as I expect it to. It can probably be optimised further but I t hink its a good start :)

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#include <iostream>

using namespace std;

int main()
{

    int number = 0; //Input Variable

    //Output test variables
    int test2 = 0;
    int test3 = 0;
    int test4 = 0;
    int test5 = 0;
    int test6 = 0;
    int test7 = 0;
    int test8 = 0;
    int test9 = 0;
    int test10 = 0;
    int test11 = 0;
    int test12 = 0;

    //Test Indicator Variables
    bool isDivBy2 = false;
    bool isDivBy3 = false;
    bool isDivBy4 = false;
    bool isDivBy5 = false;
    bool isDivBy6 = false;
    bool isDivBy7 = false;
    bool isDivBy8 = false;
    bool isDivBy9 = false;
    bool isDivBy10 = false;
    bool isDivBy11 = false;
    bool isDivBy12 = false;
    bool isPrime = false;

    //Get input from console
    cout << "Please enter a whole number: ";
    cin >> number;
    while(!cin)
    {
        cout << "That was not a number! Please enter a whole number:  ";
        cin.clear();
        cin.ignore();
        cin >> number;
    }
    //set STD::|BoolApoha to return True / False
    std::cout << std::boolalpha;

    //Test for divisibility by 2
    test2 = number;
    test2 = test2 % 2;
    if (test2 == 0){
            isDivBy2 = true;
    }
    cout << endl;
    cout << "Divisible by 2:     " << isDivBy2 << endl;
    //Test for divisibility by 3
    test3 = number;
    test3 = test3 % 3;
    if (test3 == 0){
            isDivBy3 = true;
    }
    cout << "Divisible by 3:     " << isDivBy3 << endl;
        //Test for divisibility by 4
    test4 = number;
    test4 = test4 % 4;
    if (test4 == 0){
            isDivBy4 = true;
    }
    cout << "Divisible by 4:     " << isDivBy4 << endl;
        //Test for divisibility by 5
    test5 = number;
    test5 = test5 % 5;
    if (test5 == 0){
            isDivBy5 = true;
    }
    cout << "Divisible by 5:     " << isDivBy5 << endl;
        //Test for divisibility by 6
    test6 = number;
    test6 = test6 % 6;
    if (test6 == 0){
            isDivBy6 = true;
    }
    cout << "Divisible by 6:     " << isDivBy6 << endl;
    //Test for divisibility by 7
    test7 = number;
    test7 = test7 % 7;
    if (test7 == 0){
            isDivBy7 = true;
    }
    cout << "Divisible by 7:     " << isDivBy7 << endl;
    //Test for divisibility by 8
    test8 = number;
    test8 = test8 % 8;
    if (test8 == 0){
            isDivBy8 = true;
    }
    cout << "Divisible by 8:     " << isDivBy8 << endl;
    //Test for divisibility by 9
    test9 = number;
    test9 = test9 % 9;
    if (test9 == 0){
            isDivBy9 = true;
    }
    cout << "Divisible by 9:     " << isDivBy9 << endl;
        //Test for divisibility by 10
    test10 = number;
    test10 = test10 % 10;
    if (test10 == 0){
            isDivBy10 = true;
    }
    cout << "Divisible by 10:    " << isDivBy10 << endl;
    //Test for divisibility by 11
    test11 = number;
    test11 = test11 % 11;
    if (test11 == 0){
            isDivBy11 = true;
    }
    cout << "Divisible by 11:    " << isDivBy11 << endl;
    //Test for divisibility by 12
    test12 = number;
    test12 = test12 % 12;
    if (test12 == 0){
            isDivBy12 = true;
    }
    cout << "Divisible by 12:    " << isDivBy12 << endl;
    //Testing for Prime
    if(isDivBy2 | isDivBy3 | isDivBy4 | isDivBy5 | isDivBy6 | isDivBy7 | isDivBy8 | isDivBy9 | isDivBy10 | isDivBy11 | isDivBy12 == false){
        isPrime = true;
        cout << endl <<"Only divisible by one and itself, number is prime" << endl;
    }
}
Well, for a start, there's a lot of of repeated code there. You use the same algorithm for every different number you test, so you could easily have a single function for that, rather than essentially duplicating the same code 11 times.
Getting somewhere (I think!)

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
#include <iostream>

using namespace std;
void Divisibility(int number);
int main()
{
int number, flag = 0;

  //Get input from console
    cout << "Please enter a whole number: ";
    cin >> number;
    while(!cin)
    {
        cout << "That was not a number! Please enter a whole number:  ";
        cin.clear();
        cin.ignore();
        cin >> number;
    }
    //set STD::|BoolApoha to return True / False
    std::cout << std::boolalpha;
    cout << endl;
flag = Divisibility(number);
Divisibility(int number);
return 0;

}

void Divisibility(int number)
	{
	int i;
	bool isFactor = true;
	for (i = 1; i <= 12; ++i)
		if (number % i != 0)
		{
		isFactor = false;
		cout << "divisible by " << i << "     " << isFactor << endl;
			break;
		}
	}


Unfortunately not compiling:

||=== Build: Debug in DivisibilityLoop (compiler: GNU GCC Compiler) ===|
D:\C++\DivisibilityLoop\main.cpp||In function 'int main()':|
D:\C++\DivisibilityLoop\main.cpp|22|error: void value not ignored as it ought to be|
D:\C++\DivisibilityLoop\main.cpp|23|error: expected primary-expression before 'int'|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Line 22flag = Divisibility(number);
A void function doesn't have a return type, so you can't assign it to a variable.
I was attempting to follow https://www.programiz.com/cpp-programming/user-defined-function-types#yes_argument_no_return but its not clear where the "n" variable comes from as it is not declared. What changes do I need to make to the code to get it to work correctly?

n is the name of the parameter. In main it was called with num: prime(num);

Remove line 22 and change line 23 to Divisibility(number);
I think I have the program flowing correctly now, but the loop is not working as i'd hoped:

expectation:

Please enter a whole number: 144

Divisible by 2 true
Divisible by 3 true
Divisible by 4 true
Divisible by 5 false
Divisible by 6 true
Divisible by 7 false
Divisible by 8 true
Divisible by 9 true
Divisible by 10 false
Divisible by 11 false
Divisible by 12 true

Reality:

Please enter a whole number: 144

divisible by 5 false

Process returned 0 (0x0) execution time : 4.803 s

I can see WHY the loop is going wrong, but I cannot see HOW it is going wrong (the loop is only picking out false cases then stopping at the first).

Where have I tripped up?

1
2
3
4
5
6
7
8
9
10
11
12
13
void Divisibility(int number)
	{
	int i;
	bool isFactor = true;
	for (i = 1; i <= 12; ++i)
		if (number % i != 0)
		{
		isFactor = false;

		}
		cout << "divisible by " << i << "     " << isFactor << endl;
			break;
	}
Last edited on
If you don't use braces to define a block of code to iterate in your loop, then only the first statement will be part of the loop. This means that the only lines that iterate as part of the loop are:

1
2
3
4
5
		if (number % i != 0)
		{
		isFactor = false;

		}


Lines 11 and 12 aren't part of the loop - they only execute after the loop has finished.

And if you did use braces to make them part of the loop, then that break statement would exit the loop during the first iteration anyway, so you'd still only get one line output.
Might I also suggest the following correction:
1
2
3
4
5
6
7
8
9
#include <limits>
//...
    while(!cin)
    {
        cout << "That was not a number! Please enter a whole number:  ";
        cin.clear();
        cin.ignore(std::numeric_limits<std::streamsize> ::max(), '\n');
        cin >> number;
    }


This will ignore the entire string entered into std::cin until the newline character. Your implementation will go through the stream multiple times character by character and output to the screen multiple times, when all you need is to output the error message once.
Topic archived. No new replies allowed.