1

I can't seem to find what's wrong with my code, I'm trying to end the loop once the answers is equals to 0 but it keeps going on an infinite loop.

#include <iostream>
int main() {
    using namespace std;
    int x, remainder;
    cout << "please enter a positive integer number: " << endl;
    string tab;
    tab = '\t';
    cin >> x;
    remainder = x % 2;

    do{
        while ( x % 2 != 0)
        {
            cout << x << " is odd" << tab << "Subtract 1" << tab << " Half of " << x - 1 << " is " << x / 2;
            x = (x - 1) / 2;
            cout  << endl;

        }

        while (x % 2 == 0)
        {
            cout << x << " is even" << tab << "Subtract 0" << tab << "Half of  " << x << " is " << x / 2;
            x = x / 2;
            cout << endl;
        }

    }
    while (x >= 0);  
}

halfer
  • 19,824
  • 17
  • 99
  • 186
GG Bro
  • 65
  • 9

1 Answers1

2

There are, essentially, two problems in your code, both of which, in themselves, will make your loop run endlessly.

Starting from the outside and working inwards: The test at the end of your (outer) while loop will always be "true", as you have while (x >= 0); so, even when x gets to zero (as it will), the loop will keep running! (And, once x is zero it will remain zero!)

Second, the two 'inner' while loops shouldn't be loops at all! You want one or the other 'block' to run once only for each main loop - so use an if ... else structure.

The following is a corrected version of your code:

#include <iostream>

int main() {
//  using namespace std; // Generally, not good practice (and frowned-upon here on SO)
    using std::cin;  using std::cout; using std::endl; // Use only those you want to!
    using std::string;
    int x, remainder;
    cout << "please enter a positive integer number: " << endl;
    string tab;
    tab = '\t';
    cin >> x;
    remainder = x % 2;

    do {
        if (x % 2 != 0)
        {
            cout << x << " is odd" << tab << "Subtract 1" << tab << " Half of " << x - 1 << " is " << x / 2;
            x = (x - 1) / 2;
            cout << endl;
        }
        else // if (x % 2 == 0) ... but we don't need to test this again.
        {
            cout << x << " is even" << tab << "Subtract 0" << tab << "Half of  " << x << " is " << x / 2;
            x = x / 2;
            cout << endl;
        }
    } while (x > 0); // You run forever if you have x >= 0!
    return 0;
}

There are a few other things that could be changed to make the code more "efficient," but I'll let you peruse the MNC (Minimum Necessary Change) before we start editing towards a BPC (Best Possible Code)!

EDIT: OK, due to 'peer pressure' from comments , I'll put in a suggested BPC now:

#include <iostream>

int main() {
    using std::cin;  using std::cout; using std::endl; // Use only those you want to!
    int x;// , remainder; // "remainder" is never used, so we can discard it!
    cout << "Please enter a positive integer number: " << endl;
    cin >> x; // Not critical, but I like to put such a cin right after the prompting cout.
    std::string tab{ "\t" }; // Let's initialise more directly!
    do {
        // As there is only one line (now) inside each if/else block, we can leave out the {} ...
        if (x % 2 != 0)
            cout << x << " is odd" << tab << "Subtract 1" << tab << "Half of " << x - 1 << " is " << x / 2;
        else
            cout << x << " is even" << tab << "Subtract 0" << tab << "Half of  " << x << " is " << x / 2;
        // We can put the following two line outside the tests, as they will be the same in both cases:
        x = x / 2; // When x is ODD, this will give the SAME answer as x = (x - 1)/2 (as you noticed in your first cout)
        cout << endl;
    } while (x > 0); // You run forever if you have x >= 0!
    return 0;
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • It's also worth pointing out that there's no need to subtract 1 before dividing: integer truncation takes care of that. – paddy Oct 10 '19 at 05:55
  • @paddy This is coming in the next edit (BPC - really)! But the OP has actually realized this, if you look at the first `cout` line in his code. – Adrian Mole Oct 10 '19 at 06:08
  • @paddy See edit! (It was already typed like that - honest, guv!) A minor improvement (depending on taste) would be to change `x = x / 2;` to `x /= 2;` but that's not critical, IMHO. – Adrian Mole Oct 10 '19 at 06:17
  • Thanks a ton, I made a few correction to my code and its looking good to go. I wonder why using namespace std isn't a good practice since my current professor in college told us that'ts its better, – GG Bro Oct 10 '19 at 06:54
  • @GGBro Apparently, lots of College Profs teach folks to use it! However, for an informed discussion, see here: https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice – Adrian Mole Oct 10 '19 at 07:06