Hi there,

I have to make a function valid_dice_selection that helps another function select_dice to check the length of a string and besides that it has to check that the characters in that string only include 'x', 'X' and '-'.
Ive gotten so far:

Im not sure how to check wether input(which is the string i have to work with i suppose) only contains 'x', 'X' or '-'.
I was thinking with some sort of for loop (hence the input) and checking the value of each array element one by one?


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;
}

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);
	}
	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;
	}
}

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;
}
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.