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