Hey guys what im trying to do is write a yatzee game with C.
And im stuck already and its the start?!?!
I want the user to type there 5 numbers.
i.e My program so far does this
Please enter dice Values:
> 1 2 3 4 5
it will then print the numbers
1 2 3 4 5
now thats cool.
But i also want error messages i.e if there are too many numbers entered, or if its in the incorrect input format or incorrect values for the numbers. I.e lower then 1 or higher then 6.
so this is the program ive written so far :
#include <stdio.h>
#include <ctype.h>
int main (int argc, char **argv) {
int dice[5];
int loop;
int next;
int flag2;
int flag1;
printf("Please enter dice values:\n");
for(loop=0;loop < 5; loop++){
scanf("%d",&dice[loop]);
}
for (loop=0,flag2=0,flag1=0;loop<5 && flag2!=5 && flag1<1;loop++){
if(dice[0]>6||dice[1]>6||dice[2]>6||dice[3]>6||dice[4]>6||dice[0]<1||dice[1]<1||dice[2]<1||dice[3]<1||dice[4]<1){
flag1++;
}
else if (dice[0]<6||dice[1]<6||dice[2]<6||dice[3]<6||dice[4]<6||dice[0]>1||dice[1]>1||dice[2]>1||dice[3]>1||dice[4]>1){
flag2++;
printf("%d ",dice[loop]);
}
}
if(dice[0]>6||dice[1]>6||dice[2]>6||dice[3]>6||dice[4]>6||dice[0]<1||dice[1]<1||dice[2]<1||dice[3]<1||dice[4]<1){
printf("Values out of output range.\n");
}
next=getchar();
if(next==' '){
next=getchar();
if(isdigit(next)){
printf("Incorrect number of values.\n");
}
}
return (0);
}
There are two things wrong with this. It prints the Value out of range Error message fine.
but for the incorrect number of values i.e sum1 enters 7 values instead of 5 like below
Please enter numbers:
1 2 3 4 5 6 7
Output:
1 2 3 4 5 Incorrect number of values?
How do i fix that????
also if its a wrong input like
1, 3 +4 6,5 2 (instead of 1 3 4 6 5 2) how would i print an error message for that Please help me.
And thanks in advance