Is anyone willing to help me craft a good explanation of why the following code is not correct, in that the author is attempting to write C++ code declaratively rather than procedurally?
const double NEWTONS_PER_POUND = 4.448;
int main()
{
double pounds, newtons;
pounds = newtons/NEWTONS_PER_POUND; /* pounds equals 'unassigned variable'/4.448 */
newtons = 10.0;
cout << pounds << endl; /* a big number, not 10.0/4.448 */
return 0;
}
The author expected cout to display a proper calculation, but instead gets a 'crazy number.'
I would explain this as "C++ is procedural, and therefore at the time of the declaration
pounds = newtons/NEWTONS_PER_POUND;
newtons has not been assigned a value.
Any better suggestions? Or an explanation why C++ isn't 'smart' enough to exhibit the behaviour the user mistakenly expected?