1

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);



 }
st4evr
  • 139
  • 1
  • 5
  • 17
  • use a break statement. – ergonaut Oct 18 '15 at 02:11
  • problem with the way you are reading is cin will take blah and give 0 ( which is divisible by 7.. ). you need to differentiate between invalid input and 0 – amdixon Oct 18 '15 at 02:12
  • Should be divisible == 0, not 1. Second, you need to check for wrong input before processing the number. Third, you can do else { } without do. You can also use % to get the remainder of a division between ints. – 4nt Oct 18 '15 at 02:21
  • see [how-to-check-if-input-is-numeric-in-c](http://stackoverflow.com/questions/5655142/how-to-check-if-input-is-numeric-in-c) for proper cin error handling – amdixon Oct 18 '15 at 02:21
  • Thank you. I decided to go a different route and update the original post. Got most of it to work, but still having issues with the "blah" as it puts it into an infinite loop. Any help with that is greatly appreciated. – st4evr Oct 18 '15 at 03:43
  • @st4evr Can you post your better, most up-to-date code? This is so easy that's it's hard to give _an_ answer without giving _the_ answer without the latest context. Also, if you program says "num is divisible by 7", do you terminate program or loop and prompt for new number? That is, only reloop on bad/non-digit input or reloop always (e.g. when user is done, s/he hits ctrl-c) – Craig Estey Oct 18 '15 at 03:57

3 Answers3

3

You want something more like:

while (true) {
    cout << "Enter a positive integer: ";
    cin >> num; 
    bool divisible = !(num % 7);
    if (divisible) {
        cout << "It's divisible by 7" << endl;
        break;
    }
    else {
        cout << "It's not divisible by 7, try again: " << endl;
    }
}
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
  • doesnt cover user input "blah" ( treats it same as a divisible by 7 number ) – amdixon Oct 18 '15 at 02:57
  • Thank you. I decided to go a different route and update the original post. Got most of it to work, but still having issues with the "blah" as it puts it into an infinite loop. Any help with that is greatly appreciated. – st4evr Oct 18 '15 at 03:43
0

While there may be several std functions that you can use, here it's probably easier to roll your own:

int
getnum(void)
{
    char chr;
    int num;

    while (1) {
        cout << "Enter a positive integer: ";

        num = 0;

        while (1) {
            cin >> chr;
            if (chr == '\n')
                break;

            if (num < 0)
                continue;

            if (chr < '0')
                num = -1;
                continue;
            }

            if (chr > '9') {
                num = -1;
                continue;
            }

            num *= 10;
            num += chr - '0';
        }

        cout << endl;

        if (num >= 0)
            break;

        cout << "Not a valid number\n";
    }

    return num;
}
Craig Estey
  • 30,627
  • 4
  • 24
  • 48
0

Moved stuff around further and final code achieved with the help and guidance of some C++ internet teachers. Thank you all for your replies and assistance!! Very much appreciated!

 #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;    
 }
st4evr
  • 139
  • 1
  • 5
  • 17