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!!
EDIT
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 below. Any help is greatly appreciated in getting that part to work.
https://www.dropbox.com/s/y6tx02nwmq1pdvr/scenario4_results.jpg?dl=0
EDIT #2
Okie dokie. I am getting a bit closer. Used 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.
https://www.dropbox.com/s/2zueqy5foijka9s/scenario5_results.jpg?dl=0
#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);
}