Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   creditcard structures in C (http://www.go4expert.com/forums/creditcard-structures-c-t20362/)

urikorulzs2 14Dec2009 08:56

creditcard structures in C
 
Heres what i need to do!!

A. Create a structure, called creditcard that contains three members: credit_card_number (integer data type), authorization_number (integer data type) and amount_to_charge (float data type):

B. Create an array called records, which contains 50 creditcard structures.

C. Using three lines of code, initialize the data for records[25] to: credit_card_number = 4111111, authorization_number=56222 and amount_to_charge = 29.95.

D. Create a pointer to creditcard and call it cardPtr. Initialize cardPtr to point to the zeroeth element of the records array.

E. Using cardPtr defined above, initialize the data for records[0] to: credit_card_number=599121, authorization_number = 89333 and amount_to_charge = 49.95.

F. Using cardPtr, print out all data contained in records[25].


Heres what i have, please help!! i ran the program, it compiles and runs fine, but the problem is, it prints out two lines of text saying only credit card number, and give random numbers each time the program is run.

Code:




int main( void )
        {
          int acctNum;    /* acctNum number */
          char lastName[ 15 ]; // account last name //
          char firstName[ 10 ]; // account first name //
          int userID;      // account user ID //
          double balance;  /* acctNum balance */
       
          FILE *cfPtr;    /* cfPtr = clients.dat file pointer */
       
          /* fopen opens file; exits program if file cannot be opened */
          if ( ( cfPtr = fopen( "account.txt", "r" ) ) == NULL ) {
              printf( "File could not be opened\n" );
          } /* end if */
 else { /* read acctNum, lastName, firstName, userID, & balance from file */
              printf( "%-10s%-10s%-12s%-9s%s\n", "Account", "LastName", "FirstName", "UserID", "Balance" );
              fscanf( cfPtr,"%d%s%s%d%lf", &acctNum, lastName, firstName, &userID, &balance );
                  /* while not end of file */
              while ( !feof( cfPtr ) ) {
                printf( "%-10d%-10s%-12s%-9s%7.2f\n", acctNum, lastName, firstName, userID, balance );
                fscanf( cfPtr, "%d%s%s%d%lf", &acctNum, lastName, firstName, &userID, &balance );
                    } /* end while */
       
              fclose( cfPtr ); /* fclose closes the file */
            } /* end else */
       
          return 0; /* indicates successful termination */
       
        } /* end main */


urikorulzs2 14Dec2009 09:40

Re: creditcard structures in C
 
sorry i put the wrong code up there, heres the right code.

Code:


        struct creditcard  {
                int cc_number;
                int auth_number;
                double amt_charge;
        };
 
                int main(void) {
       
          // Local Variables
        struct creditcard records[50];
        struct creditcard *ccPtr;

          // Begin
          records[25].cc_number = 4111111;
          records[25].auth_number = 56222;
          records[25].amt_charge = 29.95;
          ccPtr = &records[0];
          while (ccPtr!=&records[25]);{
                        *ccPtr++ = records[25];
                };
          ccPtr[0].cc_number = 599121;
          ccPtr[0].auth_number = 89333;
          ccPtr[0].amt_charge = 49.95;

          printf("Credit Card Number:%d\n", "Authorization Number:%d\n"
                    "Amount To Charge:%.2f\n\n", records[25]);
          printf("Credit Card Number:%d\n", "Authorization Number:%d\n"
                    "Amount To Charge:%.2f\n", ccPtr[0]);

          return 0;

        } // end function main


Gene Poole 15Dec2009 20:21

Re: creditcard structures in C
 
I'm confused by the assignment directives, but I think they are getting at pointer dereferencing. ccPtr can point to any element in records. Once assigned to a record, you dereference elements using the -> operator.

Try something like this:

Code:

#include <stdio.h>

struct creditcard  {
  int cc_number;
  int auth_number;
  double amt_charge;
};

int main(void) {

  // Local Variables
  struct creditcard records[50];
  struct creditcard *ccPtr;

  // Begin
  records[25].cc_number = 4111111;
  records[25].auth_number = 56222;
  records[25].amt_charge = 29.95;

  ccPtr = &records[0];  //ccPtr now points to record[0]


  ccPtr->cc_number = 599121;
  ccPtr->auth_number = 89333;
  ccPtr->amt_charge = 49.95;

  printf("Credit Card Number:%d\nAuthorization Number:%d\nAmount To Charge:%.2f\n",
    ccPtr->cc_number,
    ccPtr->auth_number,
    ccPtr->amt_charge);


  ccPtr = &records[25];  //ccPtr now points to record[25]

  printf("Credit Card Number:%d\nAuthorization Number:%d\nAmount To Charge:%.2f\n",
    ccPtr->cc_number,
    ccPtr->auth_number,
    ccPtr->amt_charge);

  return 0;

} // end function main



All times are GMT +5.5. The time now is 20:57.