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

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    your printf formats miss % in some places
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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,057
    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