Consider the following code:
float validateEntry()
{
string entry;
float value;
getline(cin, entry);
value = atof(entry.data());
return ((isNumber(entry) && value >= 0) ? i
: (cout << "Enter valid amount: ", validateEntry())
}
Why is the last line (the comma-separated expression) allowed, and are there other expressions that can be used with return statements in C++?
I’m mostly confused at the use of the comma, and wondering where this expression syntax is defined (I had no idea it existed, nor would I have known where to find out). Can I fill that last expression with an indefinite amount of code; if so, what are the limitations, requirements, etc.?
The comma operator allows you to group two otherwise-unrelated expressions. Both expressions are always evaluated, and the result is the result of the second expression. It is almost always a bad idea to use it (because it hurts readability just to save a line of code), except maybe in the top of a for-loop.