Fix C code

Discussion in 'C' started by mosdn85, Jan 1, 2013.

  1. mosdn85

    mosdn85 New Member

    Joined:
    Dec 31, 2012
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Can someone fix this?
    Code:
    #include <stdio.h>
    
    char candydata [999];
    int findAmountOfCandyCollected( int candyCollected, int candyLost , int candyate);
    
    int main(void)
    { 
        int candyCollected;
        int candyLost;
        int candyate;
    
        printf( "Please input the amount of candycollected : " );
        scanf( "%d", &candyCollected );
        printf( "Please input the amount of candylost : " );
        scanf( "%d", &candyLost );
        printf( "Please input the amount of candyate : " );
        scanf( "%d", &candyate );
        printf( "The product of your three numbers is %d\n", findAmountOfCandyCollected( candyCollected, candyLost, candyate ) );
        getchar();
    }
    
    int findAmountOfCandyCollected (int candyCollected, int candyLost, int candyate)
    {
        return candyCollected - candyLost - candyate ;
        {
            int findAmountOfCandyCollected;
    
            FILE *dataptr;
            dataptr= fopen("candydata", "w");
    
            if (dataptr !=0)
            {
                fprintf(dataptr, "%d ", findAmountOfCandyCollected);
            }
            else
    
            {
                printf("could not open the file. /n)");
            }
            fclose(dataptr);
            return 0;
        }
    }
     
  2. hobbyist

    hobbyist New Member

    Joined:
    Jan 7, 2012
    Messages:
    141
    Likes Received:
    0
    Trophy Points:
    0
    you didn't say what was wrong or what it should be doing, but your function is returning a value without ever entering the file operation.

    It looks like the function might be wanting something like below
    Code:
    int function(int a, int b, int c)
    {
       FILE *f;
       ...
       fprintf(f, "%d ", a - b - c);
       ...
       return a - b - c;
    }
    really it depends on what you're wanting to return and what you're actually trying to do.
     
  3. tulasi

    tulasi New Member

    Joined:
    Jan 2, 2013
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi Mos,

    Wish you a happy new year.

    I did not see any error in your code, other than using getchar() at the end.

    You have to use getch() instead of getchar() - Hope this should solve your problem..

    Thanks,
    Tulasi
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice