# Thread: Having issues figuring out what went wrong

1. ## Having issues figuring out what went wrong

I am new to the C language and am trying to figure out what I'm doing wrong with my code. I developed a basic function code that allows me to calculate the batting average of a hitter. My code passes every time and lists the correct number of hits and at bats, but it's not calculating the average correctly. Please help. The code is listed below.

Code:
```#include <stdio.h>
int main(void) {

/* function declaration */
double batting_avg(int hits, int atbats){return hits/atbats;}int hits, atbats, avg;
// While a positive number
while (atbats > 0)
{printf("Enter a positive integer\n: ");
scanf("%d", &atbats);
if (atbats > 0);
{printf("Batter has had %d at bats.\n: ",atbats);
scanf("%d", &hits);
if (hits < atbats);
{
printf("The batter has %d hits. \n: ",hits);
avg=hits/atbats;
printf("The batter is %d for %d, his %.3f.\n", hits,atbats,avg);
}
}
}
return 0;
}```

2. You should declare your functions outside of "main". Also, be sure your code is neatly formatted and indented. It should look something like this:

Code:
```#include <stdio.h>

/* function declaration */
double batting_avg(int hits, int atbats)
{
return hits/atbats;
}

int main(void)
{
int hits, atbats, avg;

// While a positive number
while (atbats > 0)
{
printf("Enter a positive integer\n: ");
scanf("%d", &atbats);
if (atbats > 0);
{
printf("Batter has had %d at bats.\n: ",atbats);
scanf("%d", &hits);
if (hits < atbats);
{
printf("The batter has %d hits. \n: ",hits);
avg=hits/atbats;
printf("The batter is %d for %d, his %.3f.\n", hits,atbats,avg);
}
}
}

return 0;
}```
Also, be sure to explain your problem in more detail. Telling us the input you're entering, the output you're getting, and the output you're expecting will help us help you.

And ... be sure you are compiling with full warnings enabled:

Code:
```main.c||In function 'main':
main.c|18|warning: suggest braces around empty body in an 'if' statement
main.c|22|warning: suggest braces around empty body in an 'if' statement
main.c|26|warning: format '%.3f' expects type 'double', but argument 4 has type 'int'
||=== Build finished: 0 errors, 3 warnings ===|```
First problem: You have a semi-colon after each of your if's. This is wrong.

What the logic of your code actually looks like is this:

Code:
```if (atbats > 0)
;```
A semi-colon by itself is a valid statement (called a "null statement"), that does nothing. This means that the following code will always be executed.

Thus, remove those semi-colons.

Second problem: You're trying to print a double ("%f"), but all of those variables are of type int.

This leads us to the ...

Third problem: Integer division results in a truncated quotient. In order to get a real number as a result, at least one of the operands must be a double.

If you're dividing two integer variables, the best bet is to cast one as a double:

Code:
`avg = (double)hits / atbats;`
And finally, you're declaring a function, but you're not using it. You're using the same equation in "main" instead.

3. Hi all....
Just adding , you are using "atbats" in the while condition before you have given it a value..