It is not common practice to code an assignment in an if expression i.e. if (a = 3) would assign a the value 3 and since 3 != 0, the expression is evalutated to TRUE.
But in some cases it does help to do that:
char fieldName[4];
if (ana.empty() && strcpy(fieldName,"ana"
||
cna.empty() && strcpy(fieldName,"cna"
||
fmt.empty() && strcpy(fieldName,"fmt"
)
{
cout << "Required Field not populated: " << fieldName << endl;
}
The intent of the above code is to evaluate only until it comes across an empty field, and assign that empty field name to variable fieldName.
I want to know if this is do-able. Does anyone foresee problems with this code ? I know I am putting an assignment in an if statement, and it may not be advisable. Why would it be incorrect to do so ?
I would need to write the same code in a more complex if- else construct if I did not follow this solution. Anyone has better ideas ?
But in some cases it does help to do that:
char fieldName[4];
if (ana.empty() && strcpy(fieldName,"ana"
cna.empty() && strcpy(fieldName,"cna"
fmt.empty() && strcpy(fieldName,"fmt"
{
cout << "Required Field not populated: " << fieldName << endl;
}
The intent of the above code is to evaluate only until it comes across an empty field, and assign that empty field name to variable fieldName.
I want to know if this is do-able. Does anyone foresee problems with this code ? I know I am putting an assignment in an if statement, and it may not be advisable. Why would it be incorrect to do so ?
I would need to write the same code in a more complex if- else construct if I did not follow this solution. Anyone has better ideas ?