First, dice IS plural - die is singular. There is no "dices".
Sure, you can check each char in the string, to see if it has only certain char's. A check function might look like this:
/* where 5 is the size of the input array. */
int checkStr(char input[5]) {
int i, good=0; //assume input is not good
char c;
for(i=0;input[i]!='\0';i++) { //'\0'= end of string marker char
c=input[i];
if(c!='x' && c!='X' && c!='-')
return good;
}
good=1; //if you reach here, then input must be good
return good;
}
Adak
Nearly a Posting Virtuoso
1,479 posts since Jun 2008
Reputation Points: 425
Solved Threads: 185
static int is_valid_dice_selection (char input [])
{
char input[i];
int length;
length = strlen(input);
for (i = 0; i < strlen(input)-1; i++)
{
}
return 0;
}
This function is not needed if you're going to do all the checking in select_dice() .
static void select_dice (int reroll_flags [])
{
int i;
char reroll_input [NUM_OF_DICE + 1];
do
{
printf ("Mark the dices you want to reroll with a X, the others with a -.\n");
printf ("For example, if you want to reroll the dices 2 and 5, you'll enter: \"-X--X\"\n");
scanf ("%5s", reroll_input);
Use fgets() instead. scanf() has major problems in this usage. See this Also, you don't need the next two line if you remove the function above.
}
while (! is_valid_dice_selection (reroll_input));
for (i = 0; i < NUM_OF_DICE; i++)
{
if (tolower (reroll_input [i]) == 'x')
reroll_flags [i] = 1;
else if (reroll_input [i] == '-')
reroll_flags [i] = 0;
}
}
Just before the FOR loop, set a flag to a TRUE value. Then if any of the 5 characters is not 'X', 'x', or '-', set the flag to FALSE. After checking all 5 characters, test the flag. Then you can loop back to input again if FALSE.
Alternative, move the FOR loop into is_valid_dice_selection() , with the flag.
WaltP
Posting Sage w/ dash of thyme
10,506 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944