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

1. 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. 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.

3. double percent needs a semicolon -

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

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

4. your printf formats miss % in some places

5. 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. 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.