Thread: incompatible types in assignment

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    36

    incompatible types in assignment

    Hello all,

    I am getting incompatible types in assignment on lines 144, 147, 153. Can anyone tell me what the problem is? Please be kind. I'm new to C programming. Thank you.


    hw2.c: In function `updateID':
    hw2.c:144: incompatible types in assignment
    hw2.c:147: incompatible types in assignment
    hw2.c:153: incompatible types in assignment


    Code:
    #include <stdio.h>
    
    /* structure defintions */
    struct customerData {
        int customerID;
        char firstName [ 21 ];
        char lastName [ 21 ];
        int age;
        char gender [2];
        float weight;
        float height;
        int term;
    };
    
    /* prototypes */
    void init (FILE *fPtr );
    int enterChoice( void );
    void createFile( FILE *readPtr );
    void updateID( FILE *fPtr );
    void newID( FILE *fPtr );
    void deleteID( FILE *fPtr );
    
    int main()
    {
    FILE *cfPtr; /* account.dat file pointer */
    int choice; /* user choice */
    
    /* fopens the file; exits if the file cannot be opened */
    if ( ( cfPtr = fopen( "customers.dat", "rb+" ) ) == NULL ) {
        printf( "File could not be opened.\n" );
    } /* end if */
    else {
        /* enable user to specify action */
        while ( ( choice = enterChoice() ) !=5 ) {
            switch( choice ) {
                /* create text file from record */
                case 1:
                    createFile( cfPtr );
                    break;
                /* update record */
                case 2:
                    updateID( cfPtr );
                    break;
                /* create record */
                case 3:
                    newID( cfPtr );
                    break;
                /* delete ID*/
                case 4:
                    deleteID( cfPtr );
                    break;
                /* display message if the user does not select a valid choice */
                default:
                    printf( "Sorry wrong choice.\n" );
                    break;
            } /* end switch */
        } /* end while */
        fclose( cfPtr ); /* fclose closes the file */
    } /* end else */
    
    return 0;
    } /* end main */
    
    void createFile( FILE *readPtr )
    {
        FILE *writePtr; /* customer.txt */
    
        /* create customerData with default information */
        struct customerData client = { 0, "", "", 0, "", 0.0, 0.0, 0};
    
        /* fopen opens the file; exits if the file cannot be opened */
        if ( (  writePtr = fopen( "customer.txt", "w" ) ) == NULL ) {
            printf( "File could not be opened.\n" );
        } /* end if */
        else {
            rewind( readPtr ); /* set the pointer to the beginning of the file */
            fprintf( writePtr, "%s %s %d %s %f %f %d\n",
            " Account #", "First name", "Last name", "Age", "Gender",
            "Weight", "Height", "Membership Term" );
    
            /* copy all records from random access file into text file */
            while( !feof( readPtr) ){
                fread( &client, sizeof( struct customerData ), 1, readPtr );
    
            if( client.customerID !=0 ) {
                fprintf( writePtr, "%s %s %d %s %f %f %d",
                        client.firstName, client.lastName, client.age,
                        client.gender, client.weight, client.height, client.term );
            } /* end if */
          } /* end while */
          fclose( writePtr ); /* closes the file */
        } /* end else */
    } /* end function create file */
    
    void updateID( FILE *fPtr )
    {
        int account; /* account number */
        float ID; /* account transaction */
        char firstName;
        char lastName;
        int age;
        char gender;
        float weight;
        float height;
        int term;
    
        /* create customerData with no information */
        struct customerData client = { 0, "", "", 0, "", 0.0, 0.0, 0};
    
        /* obtain number of account to update */
        printf( "Enter ID to update ( 101 - 200 ): ");
        scanf( "%d", &account );
    
        /* move file pointer to correct record in file */
        fseek( fPtr, ( account - 101 ) * sizeof( struct customerData),
        SEEK_SET );
    
        /* read record from file */
        fread( &client, sizeof( struct customerData ), 1, fPtr );
    
        /* display error if ID does not exist */
        if ( client.customerID == 0 ) {
            printf( "Account #%d had no information.\n", & account );
        } /* end if */
        else { /* update ID */
            printf( "%s %s %d %s %f %f %d\n\n",
                    client.customerID, client.firstName, client.lastName,
                    client.age, client.gender, client.weight, client.height,
                    client.term );
    
            /* request transaction */
            printf( "Please enter the account ID to update\n" );
            scanf( "%d", &ID );
            client.customerID += ID; /* account ID to update */
            printf( "Please enter the account first name to update\n" );
            scanf( "%s", &firstName );
            client.firstName += firstName;
            printf( "Please enter the account last name to update\n" );
            scanf( "%s", &lastName );
            client.lastName += lastName;
            printf( "Please enter the client's age to update\n" );
            scanf( "%d", &age );
            client.age += age;
            printf( "Please enter the client's gender to update\n" );
            scanf( "%s", &gender );
            client.gender += gender;
            printf( "Please enter the client's weight to update\n" );
            scanf( "%f", &weight );
            client.weight += weight;
            printf( "Please enter the client's height to update\n" );
            scanf( "%f", &height );
            client.height += height;
            printf( "Please enter the clien't membership term to update\n" );
            scanf( "%d", &term );
            client.term += term;
    
            printf( "%s %s %d %s %f %f %d\n\n",
                    client.customerID, client.firstName, client.lastName,
                    client.age, client.gender, client.weight, client.height,
                    client.term );
    
            /* move pointer to correct record in the file */
            fseek( fPtr, ( account - 101 ) * sizeof( struct customerData ),
            SEEK_SET );
    
            /* write updated record over the old record in file */
            fwrite( &client, sizeof( struct customerData ), 1, fPtr );
    
        } /* end else */
    } /* end funtion updateID */
    
    /* delete ID */
    void deleteID( FILE *fPtr )
    {
        struct customerData client; /* stores record read from file */
        struct customerData blankClient = { 0, "", "", 0, "", 0.0, 0.0, 0 };
    
        int ID;
    
        /* obtain number of the ID to delete */
        printf( "Enter ID number to delete ( 101 - 200 ); " );
        scanf( "%d", &ID );
    
        /* move file pointer to the correct ID */
        fseek( fPtr, ( ID - 101 ) * sizeof( struct customerData ),
        SEEK_SET );
    
        /* read record from file */
        fread( &client, sizeof( struct customerData ), 1, fPtr );
    
        /* display error if the record does not exist */
        if ( client.customerID == 0 ) {
            printf( "ID %d does not exist.\n", &ID );
        } /* end if */
        else { /* delete ID */
            /* move file pointer to correct ID in the file */
            fseek( fPtr, ( ID - 101 ) * sizeof( struct customerData ),
                    SEEK_SET );
    
            /* replace existing record with blank record */
            fwrite( &blankClient,
            sizeof( struct customerData ), 1, fPtr );
        } /* end else */
    } /* end function deleteID */
    
    void newID( FILE *fPtr )
    {
        /* create customerData with default information */
        struct customerData client = { 0, "", "", 0, "", 0.0, 0.0, 0 };
    
        int ID;
    
        /* obtain ID from the account to create */
        printf( "Enter the new ID number ( 101 - 200 ): " );
        scanf( "%d", &ID );
    
        /* move file pointer correct in record file */
        fseek( fPtr, ( ID - 1) * sizeof( struct customerData ),
        SEEK_SET );
    
        /* read record from file */
        fread( &ID, sizeof( struct customerData), 1, fPtr );
    
        /* display error if the ID already exists */
        if ( client.customerID !=0 ){
            printf( "ID #%d already contains information.\n",
                    client.customerID );
        } /* end if */
        else { /* create ID */
            /* user enters ID, first name, last name , age, gender, etc */
            printf( "Enter the First name, last name\n?" );
            scanf( "%s %s", &client.firstName, &client.lastName);
    
            client.customerID = ID;
    
            /* move file pointer to correct ID in the file */
            fseek( fPtr, ( client.customerID -1 ) *
                    sizeof( struct customerData ), SEEK_SET );
    
            /* insert ID in file */
            fwrite( &client,
                sizeof( struct customerData ), 1, fPtr );
        } /* end else */
    } /* end function newID */
    
    /* enable user to choose the input menu option */
    int enterChoice( void )
    {
        int menuOption ; /* variable to store the users choice */
    
        /* display menu options */
        printf( "\nEnter your choice:\n"
                "1 - Calculate average.\n"
                "2 - Add record.\n"
                "3 - Change record.\n"
                "4 - Delete record.\n"
                "5 - Quit.\n" );
    
        scanf( "%d", &menuOption );
        return menuOption;
    } /* end function enterChoice */

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Next time, do everyone a favor and highlight the lines in question.
    Code:
    /* structure defintions */
    struct customerData {
        int customerID;
        char firstName [ 21 ];
        char lastName [ 21 ];
        int age;
        char gender [2];
        float weight;
        float height;
        int term;
    };
    Code:
            client.firstName += firstName;
            printf( "Please enter the account last name to update\n" );
            scanf( "%s", &lastName );
            client.lastName += lastName;
            printf( "Please enter the client's age to update\n" );
            scanf( "%d", &age );
            client.age += age;
            printf( "Please enter the client's gender to update\n" );
            scanf( "%s", &gender );
            client.gender += gender;
    You can't assign arrays like that.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Posts
    36
    Why is the compiler complaining about those lines instead of the others?

  4. #4
    Registered User TheBigH's Avatar
    Join Date
    May 2010
    Location
    Melbourne, Australia
    Posts
    426
    Because the other members of the struct are numbers, not strings. And you can add 1 to a number but not to a string.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. incompatible types in assignment
    By vapanchamukhi in forum C Programming
    Replies: 6
    Last Post: 09-19-2008, 07:45 AM
  2. incompatible types in assignment
    By vapanchamukhi in forum C Programming
    Replies: 1
    Last Post: 09-18-2008, 11:35 PM
  3. Replies: 6
    Last Post: 08-23-2008, 01:16 PM
  4. Incompatible Pointer Type warnings
    By trillianjedi in forum C Programming
    Replies: 3
    Last Post: 06-11-2008, 04:16 PM
  5. incompatible pointer types
    By quasigreat in forum C Programming
    Replies: 4
    Last Post: 05-22-2008, 12:30 AM