Thread: input question

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    6

    input question

    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.
    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(" &#37;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);
    }
    i no the indentation is all over the place. sorry!!

    any help is appreciated.
    Last edited by piyush_v; 04-11-2007 at 03:13 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Question: File Input and Relative Paths
    By Ashes999 in forum C++ Programming
    Replies: 11
    Last Post: 05-23-2003, 04:21 AM
  2. Replies: 2
    Last Post: 05-12-2003, 04:40 PM
  3. quick question: File Input
    By meltingdude in forum C Programming
    Replies: 1
    Last Post: 04-08-2003, 02:02 AM
  4. Replies: 2
    Last Post: 03-15-2002, 01:45 PM
  5. Handling input errors, general question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 11-08-2001, 06:21 PM