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
ive quite easily been able to code it to check for number two but am having a few problems with 1 and 3
1) im using getchar to get the values off the user and then use an if statement with the isdigit() to filter out the digits. Porblem: 12345 is read the same way as 1 2 3 4 5 which isint in accordance to error checking method number 1
3) i tried creating a frequency arry whose components i could add and see if they were <or>5. for some reason when i try to print that array i get weird values like the following and ive got no idea why
following is my codeCode:Freq[1]= 134518840 Freq[2]= -1075437672 Freq[3]= 134513481 Freq[4]= -1208152076 Freq[5]= -1208250368 Freq[6]= -1075437640
nb: i no the formatting is wrong.Code:#define VALUES 5 #define FREQ 7 #include <stdio.h> #include <ctype.h> #include <stdlib.h> int main (int argc, char **argv) { int dice[VALUES]; int freq[FREQ]; int x = 0; int p; int c; printf ("Please enter dice values: \n"); c = getchar(); while(c !=10) { if (isdigit(c)) { dice[x] = c-48; x = x + 1; } c = getchar(); } for(x=0;x<VALUES;x++) { if (dice[x]<1 || dice[x]>6) { printf("Value Out of Range."); exit(1); } } for(x=0;x<VALUES;x++) { dice[x]=p; freq[p]++; } for (x = 0; x <VALUES; x++) { printf ("Dice [%d] = %d\n", x+1, dice[x]); } for(x=1; x<FREQ;x++) { printf("Freq[%d]= %d\n",x,freq[x]); } return (0); }
a big thankyou to anyone who can help!
piyush





Reply With Quote