Thread: Having issues figuring out what went wrong

  1. #1
    Registered User
    Join Date
    Apr 2016
    Posts
    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. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    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. #3
    Registered User
    Join Date
    Sep 2015
    Location
    Australia
    Posts
    63
    Hi all....
    Just adding , you are using "atbats" in the while condition before you have given it a value..
    Last edited by JohnGM; 04-29-2016 at 05:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with figuring out error
    By MimiTaso in forum C Programming
    Replies: 5
    Last Post: 03-03-2016, 07:02 PM
  2. Replies: 11
    Last Post: 04-22-2012, 10:41 AM
  3. figuring out code
    By Thegame16 in forum C Programming
    Replies: 16
    Last Post: 09-23-2011, 06:47 PM
  4. Help figuring out error
    By dudeomanodude in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2008, 03:20 PM
  5. Help figuring out equation.
    By gator6688 in forum C++ Programming
    Replies: 4
    Last Post: 10-05-2007, 03:17 PM