--- In [email protected], "johnmatthews2000" <jm5...@...> wrote:
>
> Haven't looked at your code yet... :-)
Doesn't look too bad. Some points:
1. You define constant strings using (for example):
const char * const filename = "rebase_stats.txt";
No need to complicate things with a pointer - just use:
const char filename[] = "rebase_stats.txt";
2. Use static where appropriate (print_help() and license).
3. You use a loop to keep generating random numbers until one is within the
desired range (and not 10). You could just generate a number within the range
directly:
base = basemin + (rand() % (1 + basemax - basemin));
BTW rand() isn't a very good generator, but I wouldn't worry about that now.
4. Instead of reading the user input character by character, just use fgets().
It is easy to break the current code:
Convert 122 decimal to base 2: 123456789012345678901234567890123456789012345
It is wrong.
Convert 122 decimal to base 892613426:
!
5. It's a good idea to include comments in your code to explain what it is
doing - obviously that doesn't help the user of the program, but it will help
you maintain it in the future.
6. You need to check whether rebase_stats.txt exists before you attempt to read
it, and create it if it doesn't. The current code returns EXIT_SUCCESS if it
can't read it - should be EXIT_FAILURE?
7. Be consistent with your indentation eg. instead of:
if (x) break;
use:
if (x)
{
break;
}
as you do elsewhere.
8. Use more spaces in expressions eg. instead of:
if ((fscanf_returned<1)&&(!feof(file_stats)))
use:
if ((fscanf_returned < 1) && !feof(file_stats))
9. It would be nice to allow the user to break out of the input loop without
entering the correct answer or pressing ctrl-c. For example, entering nothing.
10. The code in main() should be split up into functions eg. a 'get user input'
function, 'update stats' function etc.