Pointers Adresses and getting data byRef from a functions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • boltster
    New Member
    • Mar 2010
    • 7

    Pointers Adresses and getting data byRef from a functions

    OK... have been working on a class project and I am about at my wits ends...

    First using intel / XP-Pro / TurboC++4.5 (don't ask me why) / Using a dell D610

    I get the concept. I get the usage. I just can not get it to work. I believe the problem is a memory error but can not figure out why.

    I kept the code as short as possible (I won't post my entire program unless I need to...

    Here's the skinny: this is a gas and go assignment where you input day of week as 0-6, gallons, type of gas (R,U,S), and if you want your car washed. Prices are defined as #define ...

    ** Not all of code is posted to save space. Besides I want to work through the problem one function at a time...

    Code:
    #include <stdio.h>
    #include <conio.h>
    
    //input data function
    void getData (int *dNum, int *gal, char *gType, char *wAns);
    
    int main(void)
    {
    
    // DECLARE VARIABLES
       int dayNum, gallons;
       char response, gasType, washAns;
       float unitPrice, washCost, gasCost, total;
    
    getData(&dayNum, &gallons, &gasType, &washAns);  // func call getData
    
       return 0;   // program ran sucessful
    
    }
    
    // FUNCTION CALLS
    
    void getData(int *dNum, int *gal, char *gType, char *wAns)
    {
       clrscr();				// clear screen
    
    // get day of week
       printf("\nEnter day of the week (0-6)");
       printf("\nSun=0, Mon=1, Tue=2, ..., Sat=6 : ");
       scanf("%d", dNum);
    
    // get Number Of Gallons Pumped
       printf("\n\nEnter the number of gallons : ");
       scanf("%f", gal);
    
    // get Type of Gas Purchased
       printf("\n\nEnter gas type");
       printf("\n\(R, U, S or N for no gas\) : ");
       fflush(stdin);
       scanf("%c", gType);
    
    // get Car Washed?
       printf("\n\nCar Wash \(Y or N\) : ");
       fflush(stdin);	 //clear buffer for scanf
       scanf("%c", wAns);
    
    
       return;                   // return to calling place
    }
    The problem that I can see is that it input the correct data while in the function but once I leave the function all the variable change as either they are being over written in memory. We have not offically started to use malloc() at this point and all the practice assignments that I have been given, this format matches exactly. Any help would be a blessing...
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    Code:
     scanf("%f", gal);
    gal is an int*. %f says it's a float. Hmm...

    I didn't look too closely at other stuff but I owuld suggest ou initialize all variables before using them in function calls.

    Comment

    • boltster
      New Member
      • Mar 2010
      • 7

      #3
      I picked up on that about midnight last night. I have most of it working now. I still have 1 more function to work through to get the whole program working. That is the big one where I bring everything together. Will up date when I get stuck in that section.. Thanks Weak for the point out...

      Comment

      Working...