Program To Test Multiple/Divisible By 7

Pages: 12
Hello,

Beginner C++ student here, first programming class. I am trying to write a program that will check to thee is any number is divisible by seven. By that I mean any number from 0 to a billion lets say. I also need to have the program loop and ask the user to try again if a number that is not divisible by 7 is entered in if an invalid input is entered.

Example:

blah
That's not even a number.
100
That's not divisible by 7
1
That's not divisible by 7
hello
That's not even a number.
105
That's divisible by 7.

Program ends.

This is what I have so far. I can't quite figure out how to get it to stop when a correct number is entered and continue on all else. Any help is 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
49
50
51
52
 

#include <iostream>
#include <string>
#include <cstdlib> // exit EXIT_FAILURE

using namespace std;

bool die(const string & msg);

int main() {

	int num, sum = 0;

	cout << "Enter a positive integer: ";
	cin >> num; 
	cout << endl;

	unsigned divisible = (num - num / 7 * 7 == 0);

	if (divisible == 1)
		cout << "It's divisible by 7" << endl;
	else

	do {
		
		
		cin >> num; 
		cout << endl;

			if (!cin)
				cout << "That's not a number, try again: ";
			else
				cout << "It's not divisible by 7, try again: " << endl;

		
		}

	while (divisible == 0);
		

}

bool die(const string & msg)

{
	cout << "Fatal Error: " << msg << endl;

	exit(EXIT_FAILURE);

}
Last edited on
What's going on in line 19 ?
added line 23...

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
#include <iostream>
#include <string>
#include <cstdlib> // exit EXIT_FAILURE

using namespace std;

bool die(const string & msg);

int main() {

	int num, sum = 0;

	cout << "Enter a positive integer: ";
	cin >> num;
	cout << endl;

	 unsigned divisible = (num - (num / 7) * 7 == 0);

	if (divisible == 1)
		cout << "It's divisible by 7" << endl;
	else{

cout << "It's not divisible by 7, try again: " << endl;}

	do {
		cin >> num;
		cout << endl;

			if (!cin)
				cout << "That's not a number, try again: ";
			else
				cout << "It's not divisible by 7, try again: " << endl;
	}



	while (divisible == 0);
	}


bool die(const string & msg)

{
	cout << "Fatal Error: " << msg << endl;

	exit(EXIT_FAILURE);

}
Last edited on
a) Your test for divisibility is technically OK, but isn't this much nicer:

1
2
3
bool isDivisible = num%7 == 0;

	if (isDivisible)


Second, if user is allowed to type in non-numbers, then the program should first read in the user's input as a string. Then it should verify whether the string can be interpreted as a number. If it can be, convert the string to a number...

Last edited on
closed account (48T7M4Gy)
1. Is die() doing anything. If not delete it.
2. It will work either way but while ... do is better in this case especially if you make !cin a condition. You can then delete the duplication of cin>> num. I.e. you don't need the 'priming read' which I think should always be avoided.
@pearlyman

Line 19 is the logic I am using to verify if the value entered is a multiple of 7. Therefore also divisible by 7.

I was under the impression I needed to create a variable to reference in order to see if it's true or false.

Thank you @Kevin C. That is much nicer.

I removed the die() to make it easier to read.

Pics of the scenario results I get for a better idea:

If I enter a number divisible by 7:

https://www.dropbox.com/s/edzh2ozijblt1ya/scenario1_results.jpg?dl=0

If I enter a number not divisible by seven. After the first entry I have to enter a second value and the loop then triggers. Then it prompts me to try again. However, if I enter a value divisible by 7, it will still continue the loop.

https://www.dropbox.com/s/5kylqx3dizkq9xa/scenario2_results.jpg?dl=0

If I enter a non-number (blah), the loop goes infinitely.

https://www.dropbox.com/s/9vghgb4zhda4d3x/scenario3_results.jpg?dl=0

Thank you all for your responses.

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

using namespace std;

int main() {

	int num, sum = 0;

	cout << "Enter a positive integer: ";
	cin >> num;
	cout << endl;

	bool Divisible = num % 7 == 0;

	if (Divisible)
		cout << "It's divisible by 7" << endl;
	else

		do {


			cin >> num;
			cout << endl;

			if (!cin)
				cout << "That's not a number, try again: ";
			else
				cout << "It's not divisible by 7, try again: " << endl;


		}

	while (Divisible == 0);


}

Last edited on
I think you should change this line
unsigned divisible = (num - num / 7 * 7 == 0);
to KevinC's recomondation, purely because intergers have rounding problems with division and if the user was to enter a large number like 70001 it might evaluate it as being a number that is divisiable by 7.

If you don't understand KevinC's suggestion then google modulus operation.
Last edited on
closed account (48T7M4Gy)
It is 'going infinite' because you are testing !cin too late in the process. Divisibilty should only be tested after the input is valid not before.
Ok, I took a different route and created a nested loop. I got the most of it to work. Now when the number is not divisible by 7 it will continue to loop until I enter a value that is.

The only issue now is when I enter "blah", it will go on an infinite loop even though it's checking the variable before the Divisible as shown in the screenshot link. Any help is greatly appreciated in getting that part to work.

https://www.dropbox.com/s/y6tx02nwmq1pdvr/scenario4_results.jpg?dl=0

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

#include <iostream>
#include <string>

using namespace std;

int main() {

	int num = 0, sum = 0;

	
	//cin >> num;
	//cout << endl;

	unsigned Divisible = (num % 7 == 0);

		do {

			cout << "Enter an integer: ";
			cin >> num;
			cout << endl;

			while (Divisible){

				if (!cin)
				cout << "That's not a number, try again." << endl;
				break;
			}

			while
				(Divisible) {
				if (num % 7 != 0)
					cout << "It's not divisible by 7, try again." << endl;
					break;
			}

			while
				(Divisible) {
				if (num % 7 == 0)
				cout << "It's divisible by 7." << endl;
				break;
			}


		}

		while (num % 7 != 0 || !cin);


}
Last edited on
a)
that while(Divisible) construction is actually an obfuscation of a simpler construction, for example the first one:

1
2
3
4
if(Divisible){
	if (!cin)
		cout << "That's not a number, try again." << endl;
	}


b)
This can't work as long as you are not reading into string.
When you are reading into int, and there is "blah" on input, the "blah" will never get red (therefore, "blah" stays on the input)
Last edited on
closed account (48T7M4Gy)
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 <limits>

using namespace std;

int main() {

	int num = 0;
	bool keepGoing = true;

	while (keepGoing)
	{
	    cout << "Please enter an integer: ";
	    
		if (cin >> num)
			cout << "We have an integer " << num << endl;
		else
		{
			cout << "We don't have an integer" << endl;
			cin.clear();
			cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
		}
	}
}


This might provide a useful framework to build on.
Last edited on
@kemort Nice solution, although I might add that it is a bit short of perfection, at least given the original description of the problem. For example, what if a user types in "123blah"?

But, it is certainly a good starting point.
closed account (48T7M4Gy)
Try it Kevin, I did before I posted.

You'll find that the 'true' nature of stream input becomes clearer and, yes, you may need to add additional validations but I think a beginner may find lines 17 and 18 a bit daunting without making it even more obscure.

Unfortunately we've all seen academics take great pleasure in seeing freshman brought to tears over this.

PS I'll leave others to decide what state of perfection your contribution has attained.
In fact, not. The original problem does not state what the delimeters are, or that the input should be tokenized as words. So, it is ambiguous and both yours and mine interpretations are valid.
closed account (48T7M4Gy)
@Kevin

So, it is ambiguous and both yours and mine interpretations are valid.

I don't think so Kevvy.
I can't figure out what do you have in mind there, but I'm interested to hear.
Oh, I missed your 'Try it' reply.

Here is the result of the test:

123blah
We have an integer 123
We don't have an integer

Under one intepretation, this is incorrect output, while under another it is correct.
closed account (48T7M4Gy)
Under one intepretation (sic), this is incorrect output, while under another it is correct.


The stream ( via cin in this case ) is operating exactly as it is designed. There is no mis-interpretation by me or the system, only by you Kevvy. That's a fact. ;)
the cin is operating as designed, but I'm talking about problem description. Where does the problem description say that you can treat "123blah" as two separate tokens? Or that you cannot? Nowhere. Thus, it is ambiguous.
Okie dokie. I am getting a bit closer. Used the hint from Kevin and the very small bits of what I know about cin.clear() and what I could research and put the below together. I got the infinite loop to stop, but now the prog won't recognize numbers entered after that loop is triggered per screenshot in link. As always, help in getting this resolved is immensely appreciated.

Thank you!

https://www.dropbox.com/s/2zueqy5foijka9s/scenario5_results.jpg?dl=0

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

#include <iostream>
#include <string>

using namespace std;

int main() {

	int num = 0, sum = 0;

	unsigned Divisible = (num % 7 == 0);

		do {

			cout << "Enter an integer: ";
			cin >> num;
			cout << endl;

			while (Divisible){

				if (cin.peek() == '\n' && !cin.fail()) break;
					cout << "That's not a number, try again: ";
					cin.clear();
					cin.ignore(INT_MAX, '\n'); 
				
			}

			while
				(Divisible) {
				if (char(num % 7 != 0))
					cout << "It's not divisible by 7, try again." << endl;
					break;
			}

			while
				(Divisible) {
				if (num % 7 == 0)
				cout << "It's divisible by 7." << endl;
				break;
			}


		}

		while (num % 7 != 0 || !cin);



}
Last edited on
It became easier to just write the answer than to explain what is wrong.

here you go:
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>
#include <string>

using namespace std;

int main() {

	int num = 0, sum = 0;

	while(true) {

			cout << "Enter an integer: ";
			cin >> num;
			cout << endl;			

			if (cin.fail()) {					
					cout << "That's not a number, try again: ";
					cin.clear();
					cin.ignore(INT_MAX, '\n'); 
					continue;
					}			
			bool isDivisible = num%7 == 0;
									
			if (!isDivisible)
				cout << "It's not divisible by 7, try again." << endl;
			else 
				break;
		}	
		
	cout << "It's divisible by 7." << endl;	
}
Pages: 12