i have the following function that someone kindly did for me that i have modified slightly so that it gets information from the user like number of players and their names rather than just the names.
Code:
int get_input(char line[], int linesize)
{
if (!fgets(line, linesize, stdin))
{
// maybe the user triggered EOF or something, so for our purposes, bail out:
fprintf(stderr, "Error: unexpected end-of-file or read failure\n");
exit(EXIT_FAILURE);
}
char *newline = strchr(line, '\n');
if (!newline) //name too long
{
printf("Incorrect Entry: name must have a length of no more than %d characters\n",
linesize - 1);
flush_line();
return 0;
}
if (newline == line)
{
printf("Incorrect Entry: name cannot be blank\n");
return 0;
}
*newline = '\0';
return 1;
}
i pass into it the line array and size of the array so i can modify the buffer size accordingly however the if statement that checks for the \n character is fine if im looking for a string it reports the length accordingly. however to get a single number i have to pass into it line[3] which then says it wants an input of 2 characters.
is there a way i can change the argument that printf takes according to what i am looking for.
many thanks
coop