help please i have errors and dont know how to fix them new at this

This is a discussion on help please i have errors and dont know how to fix them new at this within the C Programming forums, part of the General Programming Boards category; Code: /*Dice Program*/ #include <stdio.h> #include <stdlib.h> #include <math.h> int main() { int seed, tosses, die_1, die_2, eight=0,x,sum; double percent ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    7

    help please i have errors and dont know how to fix them new at this

    Code:
    /*Dice Program*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    
    {
    int seed, tosses, die_1, die_2, eight=0,x,sum;
    double percent
    
    /*seed value*/
    printf("Enter a positive seed value: \n");
    scanf("%i",seed);
    printf("Enter the number of times you want to roll the dice: \n");
    scanf("%i",tosses);
    
    /*Equations for the 2 dice*/
    srand(seed);
    for (x=1; x<=tosses;x++)
    {
    die_1=rand()%6+1;
    die_2=rand()%6+1;
    printf("%5i %5i\n", die_1, die_2);
    sum=die_1+die_2;
    if (sum==8
            eight++);
    }
    printf("The number of eights you rolled is: 5i \n",eights);
    percent=((float)eights/tosses*100);
    printf("The percent of eights you rolled is 5.2f \n",percent);
    
    /*exit*/
    }
    Code:
     
    ERRORS
    In function `main':
    14: error: syntax error before "printf"
    28: error: syntax error before "eight"
    30: error: `eights' undeclared (first use in this function)
    30: error: (Each undeclared identifier is reported only once
    30: error: for each function it appears in.)
    31: error: `percent' undeclared (first use in this function)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,311
    It looks like all the errors boil down to three typographical errors:

    Code:
    double percent
    Where is the terminating semi-colon in the line above?

    Code:
    if (sum==8
            eight++);
    Look where the closing parenthesis is placed in the above snippet.

    Code:
    printf("The number of eights you rolled is: 5i \n",eights);
    percent=((float)eights/tosses*100);
    You used the identifier eights in the above snippet when you have been using eight all along.

    By the way, it is good to see that you posted code in code tags, but you should properly indent your code as well. You might want to increase the warning level of your compiler when compiling as there are a number of other mistakes not reported as errors that might be reported as warnings.
    Last edited by laserlight; 03-12-2009 at 03:06 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23
    double percent needs a semicolon -

    Code:
    double percent;
    if (sum==8
    eight++);

    should probably be
    Code:
    if(sum == 8) eight++;

  4. #4
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,456
    your printf formats miss % in some places
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    7

    segmentational fault?????

    ok this is the fixed code which complied and had no errors but i have a segmentation fault what is that

    Code:
    /*Dice Program*/
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main()
    
    {
    int seed, tosses, die_1, die_2, eight=0,x,sum;
    double percent;
    
    /*seed value*/
    printf("Enter a positive seed value: \n");
    scanf("%i",seed);
    printf("Enter the number of times you want to roll the dice: \n");
    scanf("%i",tosses);
    
    /*Equations for the 2 dice*/
    srand(seed);
    for (x=1; x<=tosses;x++)
    {
    die_1=rand()%6+1;
    die_2=rand()%6+1;
    printf("%5i %5i\n", die_1, die_2);
    sum=die_1+die_2;
    if (sum==8) eight++;
    }
    printf("The number of eights you rolled is: %5i \n",eight);
    percent=((float)eight/tosses*100);
    printf("The percent of eight you rolled is %5.2f \n",percent);
    
    /*exit*/
    }

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Code:
    scanf("%i",seed);
    You need to pass the address of a variable to scanf, instead of just its value. This should work:
    Code:
    scanf("%i", &seed);
    BTW, it strikes me that you don't really need sum. You could just use
    Code:
    if (die_1 + die_2 == 8) eight++;
    But if what you have is more readable, there's certainly nothing wrong with it . . . .

    Also note that you can use a seed based on the time if you want to avoid the user entering a seed, e.g.
    Code:
    #include <time.h>  /* for time() */
    
    /* ... */
    
    srand((unsigned)time(0));  /* get the current time, use it for srand seed */
    I'd also suggest returning 0 from the end of main; if you're going to the trouble of putting a comment in, this
    Code:
    return 0;
    can't be much harder.

    Finally, you should consider working on your indentation. It would make your code a lot easier to read.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21