im a 1st year uni student and im kinda stuck with this c assignment. Its supposed to be a yahtzee scoring program. read 5 values from the user and display the scoring options.
We're supposed to error check the input in 3 different ways
1)
correct format, ie the values should be separated by spaces and the number of spaces between each value doesnt matter
so 12345 is not the correct format but 1 2 3 4 5 is.
2)
range.needs to be between 1 and 6
3) frequency
there needs to be 5 dice values entered
THE PROBLEM
1)with my present code, if the user enters 12345 it is read as the correct input because im using the isdigit() function to sort out the numbers. hence 12345 is read the same way as 1 2 3 4 5. how can i get around this?
2) ive got 2 arrays, one which has the dice values and one which hs the frequency of each number on the dice. I need to find all the combinations of the dice entered and display the scoring options. ie, a straigt(1 2 3 4 5) is worth certian points, a full house( a pair and a 3 of a kind) is worth certain points. I dont no where to start with both these aspects
here is my code.
i no the indentation is all over the place. sorry!!Code:#define FREQ 7
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
int main (int argc, char **argv)
{
int VALUES=0;
int dice[VALUES];
int freq[FREQ];
int x = 0;
int p = 0;
int c;
int sum = 0;
int choice=0;
printf ("Please enter dice values: \n");
c = getchar();
while(c !=10)
{
if (isdigit(c))
{
dice[x] = c-48;
x = x + 1;
}
c = getchar();
}
VALUES = x;
for(x=0;x<VALUES;x++)
{
if (dice[x]<1 || dice[x]>6)
{
printf("Value Out of Range.\n");
exit(1);
}
}
for(x=0;x<FREQ;x++)
{
freq[x]=0;
}
for(x=0; x<VALUES;x++)
{
p=0;
p=dice[x];
freq[p]+=1;
}
for(x=1; x < FREQ; x++)
{
sum+=freq[x];
}
if ( sum<1 || sum>6)
{
printf("Incorrect number of values.\n");
exit(1);
}
printf("\nYour dice values are: ");
for(x=0;x<5;x++)
{
printf(" %d",dice[x]);
}
printf("\n\nPlease choose:\n1 -> Reroll some dice\n2 -> Reroll all dice\n3 -> Keep dice\n");
choice = getchar();
if (choice-48 <1 || choice-48>3)
{
printf("\nInvalid Choice.\n");
exit(1);
}
return (0);
}
any help is appreciated.