//Assignment 21 Program 4
#include <iostream>
usingnamespace std;
int main() {
//User Input
int input,total;
cout <<"Enter A Number To Be Divided By 2 Until 1" << endl << endl;
cout <<"Number: ";
cin >> input;
cout << "" <<endl;
do
{
total=input/2;
cout <<input<<"/2= ";
cout <<total<<endl;
break;
} while (!input==1);
system ("pause");
return 0;
}
The condition on line 24 is the same as: while ((!input) == 1) which will be false for any non-zero value of input. The break statement on line 23 makes the condition moot anyway. It always breaks out of the loop.
Also, there is no reason to have a variable named total.
Step through your code, LINE BY LINE try to understand what each statement does. What is your goal with this program? What is the program doing atm? If you still haven't seen the problem, look at line 15. Also total = input / 2 does not divide input by 2, it returns a number equivalent to input / 2. input = input / 2 will divide input by 2
You have total = input / 2 and then loop while input > 1. So this will either a) loop forever or b) never loop.
Shouldn't you be assigning input to input / 2 (shift the bit right) and possibly do something with the divided value? Otherwise you might as well just set it equal to one.
at line 15break; will be unconditionally executed, that will result in the ending of do...while loop.
and also at line 14 total = input / 2, input is not be changed, so is total , you should :
1.comment break;
2.assign input to total, before do...while structure
3.use total = total >> 1 to replace total = input/2
4. finally, loop continue condition is total > 1