Having trouble with functions

This is a discussion on Having trouble with functions within the C Programming forums, part of the General Programming Boards category; Hello guys! Okay so I'm making a student budget program for class its a basic program using functions and pointers. ...

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    21

    Having trouble with functions

    Hello guys!

    Okay so I'm making a student budget program for class its a basic program using functions and pointers. Here is the template they want us to start with:

    Code:
    main() {
    float school, living, transp, otherExp; 
    float employ, otherInc; 
    get_expenses(&school, &living, &transp, &otherExp); get_income(&employ, &otherInc); 
    display_report(school, living, transp, otherExp, employ, otherInc); return;
    } 
    
    void get_expenses(double* pSchool, double* pLiving, double* pTransp, double* pOther) {
    *pSchool = *pLiving = *pTransp = *pOther = 0; return;
    } 
    
    void get_income(double* pEmploy, double* pOther) {
    *pEmploy = *pOther = 0; return;
    } 
    
    void display_report((double School, double Living, double Transp, double OtherExp, double Employ, double OtherInc) {
    return;
    }
    I'm working on the first function on Xcode

    Code:
    void get_expenses(double* pSchool, double* pLiving, double* pTransp, double* pOther)
    And right away I'm getting an error stating: Conflicting types for 'get_expenses'

    ...? Not sure what that means.

    And after that I'm just adding the scanf's

    Code:
      printf("What is the tuition cost?");
        scanf("%d", %tuition);
        printf("What is the textbook cost?");
        scanf("%d", %textbook);
        printf("What is the school supplies cost?");
        scanf("%d", %tuition);
    But I realize those are not declared? Should I add global declarations for all my scanf's are is there a easier way I'm not getting

    The output should show:

    School Expenses: ~~~~~~~~~~~~~~
    Tuition (per semester): 1800
    Textbooks(per semester): 160 Supplies: 20
    Living Expenses: ~~~~~~~~~~~~~
    Residence/Rent/Mortgage: 400
    Utilities: 50
    Phone/Internet: 90
    Groceries/Eating out: 200
    Entertainment: 50
    Transportation: ~~~~~~~~~~~~~
    Public Transportation: 104
    Car: 0 Auto Insurance: 0
    Gas/Maintenance: 0
    Other:~~~~~
    Any other expenses: 200

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,651
    Did you prototype your functions before you tried to use them in main()? If your functions take doubles as arguments, why are you using and passing floats?

    Better go back to your book and read about scanf too.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    21
    Quote Originally Posted by rags_to_riches View Post
    Did you prototype your functions before you tried to use them in main()? If your functions take doubles as arguments, why are you using and passing floats?

    Better go back to your book and read about scanf too.
    Sorry realized its a & not a % - typo.

    Oh thats where the problem is, can you give me an example of how to prototype,

    void get_expenses(double* pSchool, double* pLiving, double* pTransp, double* pOther){

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    2,435
    can you give me an example of how to prototype
    Code:
    #include <stdio.h>
    
    int addValues(int x, int y);  // <-- function prototype/declaration - note the semi-colon
    
    int main(void)
    {
        int sum;
    
        sum = addValues(2,3);
        printf("Sum is %d\n",sum);
        return 0;
    }
    
    int addValues(int x, int y)  // <-- function definition - note no semi-colon here
    {
        return x+y;
    }
    A function only has to be prototyped before "main()" (as in this example) if it is defined after "main()"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I am having trouble with friend functions
    By stefanyco in forum C++ Programming
    Replies: 16
    Last Post: 10-20-2011, 03:24 AM
  2. having trouble with functions
    By idfjvafnv in forum C Programming
    Replies: 12
    Last Post: 10-07-2011, 10:03 AM
  3. more trouble with functions
    By volk in forum C++ Programming
    Replies: 3
    Last Post: 03-24-2003, 03:04 AM
  4. trouble with functions
    By volk in forum C++ Programming
    Replies: 9
    Last Post: 03-23-2003, 02:35 PM
  5. trouble with functions with &
    By diaperdandy in forum C++ Programming
    Replies: 4
    Last Post: 12-25-2001, 09:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21