Any specific reason why you chose to recurse on Problem 1 - Line 11?
I thought the 'if the attempt to read a valid number fails, clean up and try again' logic bacomes more obvious with recursion.
.
.
If the user enters a wrong input 5 times, then that function will return 6 times. I would suggest a do while to avoid that.
Performance is irrelevant in this situation; in the time it takes the user to enter one number, a million instructions can be executed.
But just for the sake of argument, let us say that performance is absolutely critical.
The last line in the function is: return int_in_range_from_stdin( min, max ) ;
This is classic tail call recursion, and every mainstream compiler knows how to perform TCO.
For example, with gcc, the tail recursive version of a function typically produces slightly tighter code than the iterative version. For instance, 13 instructions vs. 15 for:
// invariant: cstr is a string of decimal literals
int recursive_decimal_string_to_number( const char* cstr, int n = 0 )
{
if( cstr[0] == 0 ) return n ;
return recursive_decimal_string_to_number( cstr+1, n*10 + cstr[0] - '0' ) ;
/* g++ 4.8 -O3
__Z34recursive_decimal_string_to_numberPKci:
movl 4(%esp), %ecx
movl 8(%esp), %eax
movsbl (%ecx), %edx
testb %dl, %dl
je L2
L6:
addl $1, %ecx
leal (%eax,%eax,4), %eax
leal -48(%edx,%eax,2), %eax
movsbl (%ecx), %edx
testb %dl, %dl
jne L6
L2:
rep
ret
*/
}
// invariant: cstr is a string of decimal literals
int iterative_decimal_string_to_number( const char* cstr )
{
int n = 0 ;
for( int i = 0 ; cstr[i] != 0 ; ++i ) n = n * 10 + cstr[i] - '0' ;
return n ;
/* g++ 4.8 -O3
__Z34iterative_decimal_string_to_numberPKc:
LFB1:
movl 4(%esp), %ecx
movsbl (%ecx), %edx
testb %dl, %dl
je L13
addl $1, %ecx
xorl %eax, %eax
L12:
addl $1, %ecx
leal (%eax,%eax,4), %eax
leal -48(%edx,%eax,2), %eax
movsbl -1(%ecx), %edx
testb %dl, %dl
jne L12
rep
ret
L13:
xorl %eax, %eax
ret
*/
}