Like Tree2Likes
  • 1 Post By Matticus
  • 1 Post By anduril462

Printf ints that are modified in another function?

This is a discussion on Printf ints that are modified in another function? within the C Programming forums, part of the General Programming Boards category; Hello again, I'm having a terrible time getting this to work. Through the generous help of people here on the ...

  1. #1
    Who
    Who is offline
    Registered User
    Join Date
    Jan 2013
    Posts
    21

    Printf ints that are modified in another function?

    Hello again, I'm having a terrible time getting this to work. Through the generous help of people here on the forums, I have gotten it to record the date and I can printf it either on the same function, or in the main(). However, one of the requirements I must adhere to is to printf the statement in a brand new function, but when I do that, it just doesn't work. Heres what I mean:

    Code:
    #include <stdio.h>
    #define TICKER "LRCX"
    #define INVESTMENT_AMOUNT "10,000.00"
    
    
    //Prototypes
    int getdate(int* month1,int* day1,int* year1,int* month2,int* day2,int* year2);
    float getprice(float* BPrice, float* SPrice);
    void printdate(int month1, int day1, int year1);
    
    
    
    
    int month1, day1, year1, month2, day2, year2;
    
    
        //MAIN
    int main()
    {
    
    
    //Local Declarations
        float BPrice;
        float SPrice;
    
    
    //Starting Functions
        getdate(&month1, &day1, &year1,&month2, &day2, &year2);
        getprice(&BPrice, &SPrice);
        printf("\n");
    
    
        printdate1(&month1,&day1,&year1);
        printf("Stock: " TICKER "\n");
        printf("Amount Invested: " INVESTMENT_AMOUNT "\n");
        return 0;
    }//END OF MAIN
    
    
    
    
    //BUYING - SELLING DATES
    int getdate(int* month1,int* day1,int* year1,int* month2,int* day2,int* year2)
    {
    //Local Declarations
    
    
        printf("Enter purchase date as MM/DD/YY: ");
        scanf("%2d/%2d/%2d", month1, day1, year1);
    
    
        printf("Enter the sell date as MM/DD/YY: ");
        scanf("%2d/%2d/%2d",month2, day2, year2);
    
    
    }
    
    
    
    
    //Asking for buying - selling prices
    float getprice(float* BPrice, float* SPrice)
    {
        printf("Enter purchase price: $");
        scanf("%f",&BPrice);
    
    
        printf("Enter selling price: $");
        scanf("%f",&SPrice);
    }
    
    
    void printdate1(int month1, int day1, int year1)
    {
        printf("Purchase date: %d/%d/%d\n", month1, day1, year1);
        return;
    }
    I've tried many things, including defining the ints on the outside of main(), using the int*, without it, and many many other things.
    It comes out like this:


    Enter purchase date as MM/DD/YY: 09/15/92
    Enter the sell date as MM/DD/YY: 10/15/92
    Enter purchase price: $23
    Enter selling price: $24


    Purchase date: 4214864/4214884/4214880
    Stock: LRCX
    Amount Invested: 10,000.00


    Process returned 0 (0x0) execution time : 9.524 s
    Press any key to continue.



    Grr. Any suggestions? Thanks a bazillion

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    2,706
    Code:
    void printdate(int month1, int day1, int year1);
    This function is expecting integers to be passed to it.

    Code:
    printdate1(&month1,&day1,&year1);
    But you're passing the address of the integer variables instead.

    [edit] Since you're passing around variables between functions, there's no need to keep those variables global. Why not just declare them in "main()"?
    Who likes this.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,676
    Compile with warnings turned all the way up, then fix them all. Here's what I get:
    Code:
    $ make foo
    gcc -Wall -Wunreachable-code -ggdb3 -std=c99 -pedantic  -lm -lpthread  foo.c   -o foo
    foo.c: In function ‘main’:
    foo.c:29: warning: implicit declaration of function ‘printf’
    foo.c:29: warning: incompatible implicit declaration of built-in function ‘printf’
    foo.c:32: warning: implicit declaration of function ‘printdate1’
    foo.c: In function ‘getdate’:
    foo.c:47: warning: incompatible implicit declaration of built-in function ‘printf’
    foo.c:48: warning: implicit declaration of function ‘scanf’
    foo.c:48: warning: incompatible implicit declaration of built-in function ‘scanf’
    foo.c: In function ‘getprice’:
    foo.c:63: warning: incompatible implicit declaration of built-in function ‘printf’
    foo.c:64: warning: incompatible implicit declaration of built-in function ‘scanf’
    foo.c:64: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘float **’
    foo.c:68: warning: format ‘%f’ expects type ‘float *’, but argument 2 has type ‘float **’
    foo.c: At top level:
    foo.c:72: warning: conflicting types for ‘printdate1’
    foo.c:32: note: previous implicit declaration of ‘printdate1’ was here
    foo.c: In function ‘printdate1’:
    foo.c:74: warning: incompatible implicit declaration of built-in function ‘printf’
    foo.c: In function ‘getprice’:
    foo.c:69: warning: control reaches end of non-void function
    foo.c: In function ‘getdate’:
    foo.c:55: warning: control reaches end of non-void function
    Lines 29,47,48,63,64,74: You need to #include <stdio.h> if you want to use printf/scanf. Without it, the compiler can't check whether you're calling them correctly, which leads to the next item.
    Lines 64, 68: BPrice and SPrice are already pointers to float, no need to use the & (actually, it's incorrect to use it).
    Lines 32,72: Your function declaration/prototype, the definition/implementation, and all calls to a function must match the return type, and number and types of parameters. Don't pass int pointers (the address of month, day or year), when you just need a plain int. Drop the & from all the params on line 32. Also, why do you have a prototype for printdate, and you implement/call printdate1​. What's the 1 for?
    Lines 55,69: If you aren't returning anything, declare your function to have a return type void. Otherwise, return a value of the correct type. In this case, I would have getdate and getprice return void. Also, there's no need for an return; statement as the last line of a function returning void, it's implied.

    Also, don't use global variables. Read this: Global Variables Are Bad. Declare them in the appropriate function (probably main in this case), and pass them around.
    Who likes this.

  4. #4
    Who
    Who is offline
    Registered User
    Join Date
    Jan 2013
    Posts
    21
    You're right, it was just of the many things I've tried to get it to work. Thanks for helping out! I'm also trying to do it for another function and I'm following the steps you've shown, passing the integers, not the address.
    Code:
    int getdate(int* month1,int* day1,int* year1,int* month2,int* day2,int* year2);
    float getprice(float* BPrice, float* SPrice);
    void printdate1(int month1, int day1, int year1);
    void printprice1(float BPrice);
    
    
    
    
    
    
        //MAIN
    int main()
    {
    
    
    //Local Declarations
        float BPrice;
        float SPrice;
        int month1, day1, year1, month2, day2, year2;
    
    
    //Starting Functions
        getdate(&month1, &day1, &year1,&month2, &day2, &year2);
        getprice(&BPrice, &SPrice);
        printf("\n");
    
    
        printdate1(month1,day1,year1);
        printf("Stock: " TICKER "\n");
        printf("Amount Invested: " INVESTMENT_AMOUNT "\n");
        printprice1(BPrice);
        return 0;
    }//END OF MAIN
    
    void printprice1(float BPrice)
    {
        printf("Purchase share price: $%f\n", BPrice);  // <--
    }
    I feel like the answer is what you just said, but I can't do it. Ultra noob alert! :/



    [edit] DOH! Thanks for showing me anduri 462! I realized now what went wrong. YAY
    Last edited by Who; 02-13-2013 at 02:06 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. printf-like function ?
    By Algar in forum C Programming
    Replies: 3
    Last Post: 01-02-2013, 01:35 PM
  2. Strange printf function
    By yougene in forum C Programming
    Replies: 4
    Last Post: 03-04-2008, 10:24 AM
  3. printf like function
    By tyouk in forum C Programming
    Replies: 5
    Last Post: 11-17-2003, 07:26 PM
  4. printf like function
    By argon in forum C Programming
    Replies: 2
    Last Post: 10-07-2003, 04:12 PM
  5. Modified clrscr function
    By GaPe in forum C Programming
    Replies: 2
    Last Post: 06-01-2002, 05:07 AM

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