Well, what I would do - indeed, have done, on occasion - is add a 'comment' if possible when I post an answer to the original question. So, for example, if a user posted this code:
#include <iostream>
using namespace std;
int main()
{
int n
cout << "Enter a number: " << endl;
cin >> n;
do {
n /= 2;
cout << "Half of number is: << n << endl;
} while (n >= 0);
cout << "Done!" << endl;
return 0;
}
And had asked why the loop was running for ever, then I would explain that the problem was the comparison, n >= 0
is always going to be true
and I would offer a fix, posted as a 'working' code section.
In that posted code, I may also add some other comments, remarks, or suggestions. Maybe like this:
#include <iostream>
//using namespace std; // This is considered 'bad form' (post a link to SO discussion)
using std::cout; using std::cin; using std::endl; // Better! Only "using" what you use!
int main()
{
int n
cout << "Enter a number: " << endl;
cin >> n;
do {
n /= 2;
cout << "Half of number is: << n << endl;
} while (n > 0); // MAIN PROBLEM: n >= 0 will always be TRUE! When n reaches 0 it will remain at that value!
cout << "Done!" << endl;
return 0;
}
You can see this philosophy in action here.