CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13

Thread: problem with c

  1. #1
    Join Date
    Apr 2007
    Posts
    8

    problem with c

    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
    Code:
    Freq[1]= 134518840
    Freq[2]= -1075437672
    Freq[3]= 134513481
    Freq[4]= -1208152076
    Freq[5]= -1208250368
    Freq[6]= -1075437640
    following is my code
    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);
    }
    nb: i no the formatting is wrong.

    a big thankyou to anyone who can help!

    piyush
    Last edited by piyush_v; April 10th, 2007 at 01:27 PM.

  2. #2
    Join Date
    Mar 2001
    Posts
    2,527

    Re: problem with c

    Well I noticed that your variable p is never #1 initialized and never #2 set anywhere.

    If your using old MS compilers, this can lead to undefined behavior.

    Additionally if your not, p will alwayz be 0.

    The problem with C is that you didnt create it?
    ahoodin
    To keep the plot moving, that's why.

  3. #3
    Join Date
    Apr 2007
    Posts
    8

    Re: problem with c

    so if i interpret you right you are saying is in the beginning i should set p to a value like ive done with x?

  4. #4
    Join Date
    Mar 2001
    Posts
    2,527

    Re: problem with c

    Read my posting again.

    Where is the value of p set anywhere in your program?
    ahoodin
    To keep the plot moving, that's why.

  5. #5
    Join Date
    Apr 2007
    Posts
    8

    Re: problem with c

    Code:
    for(x=0;x<VALUES;x++)
    {      
                    dice[x]=p;
                    freq[p]++;
    }
    no?

  6. #6
    Join Date
    Mar 2001
    Posts
    2,527

    Re: problem with c

    incorrect. to initialize the value of p you have to:

    Code:
    int p = 0;//initialize variable to zero
    and later to set the value of p do something like:

    Code:
    p=10;//set the value of p to 10 with an assignment statement
    //or 
    p++; //increment the value of p
    //or
    p+=100;//add 100 to the current value
    in some compilers p is an undefined value....as i said.

    Think about what p should be.
    ahoodin
    To keep the plot moving, that's why.

  7. #7
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: problem with c

    I think this is "cant see the forest for the trees..."
    Code:
     dice[x]=p;
    should be
    Code:
      p=dice[x];
    As for the frequencies, you never init the elements to 0.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  8. #8
    Join Date
    Mar 2001
    Posts
    2,527

    Re: problem with c

    But that is his job...not ours...he has to think about his HW right?
    ahoodin
    To keep the plot moving, that's why.

  9. #9
    Join Date
    Apr 2007
    Posts
    8

    Re: problem with c

    Code:
    	for(x=0;x<FREQ;x++)
    {
    		freq[x]=0;
    }	
    
    	for(x=0; x<VALUES;x++)
    {
    		p=0;
    		p=dice[x];
    		freq[p]+=1;
    this seems to have solved the problem.
    anyone got a clue asto how i can get around the problem in error checking step one?

    thanks ahoodin and TheCPUWizard for your help! (Y)

  10. #10
    Join Date
    Apr 2007
    Location
    Wisconsin
    Posts
    14

    Re: problem with c

    For your first problem, you could read in ints instead of chars. This would force the user to space them out, it would also make it so you don't have deal with all that c-48 inconvenience. The downside is that you need to do some error checking to make sure that they don't input a character.

    instead of c = getchar(), you would use
    cin >> c;

  11. #11
    Join Date
    Jul 2001
    Location
    Otaki, New Zealand
    Posts
    303

    Re: problem with c

    cin is for 'C++'. The poster said originally that it's a 'C' assignment.

  12. #12
    Join Date
    Mar 2002
    Location
    St. Petersburg, Florida, USA
    Posts
    12,116

    Re: problem with c

    for #3, just check x. make sure it does not get too large inside the loop, and make sure it is not too small when you leave the loop.

    for #1, just do value checking on the ch.
    TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
    2008, 2009,2010
    In theory, there is no difference between theory and practice; in practice there is.

    * Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
    * How NOT to post a question here
    * Of course you read this carefully before you posted
    * Need homework help? Read this first

  13. #13
    Join Date
    Apr 2007
    Posts
    8

    Re: problem with c

    by checking values of ch ill be able to tell if its a space or a number but what if the user enters 12345, both those conditions are true and itll accept it. how can i differentiate between no space between 2 numbers and a space between them.

    this is what i could come up with??
    it can identify all the illegal delimiters the user might use except when the input is 12 33 5 etc ie 2 values not separated by gaps
    Code:
    if ( (isdigit(c)) || isspace(c)) {
    	blah, continues with adding values to array
    	}
    else
    {
    printf("Incorrect input format");
    exit(1);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)