// Lab5.cpp // Initial (but buggy) code #include using namespace std; int main() { const int LARGEST_INT = 0x7fffffff; // Repairing an endless loop at runtime // loop should display 0 to 9 (iterate 10 times), but never moves beyond 0 cout << "\nRepairing a badly written counting loop\n"; int nIncrementValue = 0; // silly value, but let's see the behaviour int nCount = 0; while (nCount < 10) { cout << nCount << endl; nCount = nCount + nIncrementValue; } // Displays the powers using the int data type // It starts out fine, but then tumbles into an endless loop, displaying 0's cout << "\nRepairing a badly written Powers of 2 loop\n"; int nPowersOf2=1; cout << "Bytes for 'nPowersOf2': " << sizeof(nPowersOf2) << ". Maximum value for an int: " << LARGEST_INT << endl; while (nPowersOf2 < LARGEST_INT) { nPowersOf2 = nPowersOf2*2; cout << nPowersOf2 << endl; } // Displays the powers using the unsigned char data type // But it displays odd-looking characters rather than numbers const char LARGEST_CHAR = 0x7f; cout << "\nRepairing a badly written Powers of 2 loop\n"; unsigned char cPowersOf2=1; cout << "Bytes for 'cPowersOf2': " << sizeof(cPowersOf2) << ". Maximum value for a char: " << LARGEST_CHAR << endl; while (cPowersOf2 < LARGEST_CHAR) { cPowersOf2 = cPowersOf2*2; cout << cPowersOf2 << endl; } }