Anderson Lizabeth
Anderson Lizabeth

Reputation: 1

File.cpp:148: error: expected primary-expression before ‘.’ token DIFFERENT SYMBOL

I'm struggling to finish this code.

#include (sorry but it won't show up the #include such as stdio.h AND OTHERS) But this is not the problem.


using namespace std;


struct CustomerFile {
    int arrivalTime;

    string driverfName,
        driverlName,
        typeOfDriver,
        driverLicNumber,
        vehicleMake,
        vehicleModel,
        Lot_taken,
        vehicleRegNumber,
        attendantName,
        ParkingArea,
        Comments,
        checkOutDateTime,
        checkInDateTime;
};

int arrivalTime;

string driverfName,

    driverlName,
    typeOfDriver,
    driverLicNumber,
    vehicleMake,
    vehicleModel,
    Lot_taken,
    vehicleRegNumber,
    attendantName,
    ParkingArea,
    Comments,
    checkOutDateTime,
    checkInDateTime;

int main(int argc, char * * argv) {

    FILE * cfPtr;

    if ((cfPtr = fopen("CustomerFile.dat", "rb+")) == NULL) {
        printf("file could not be opened");
    } else {
        printf("\nFile is Written to");
        printf("\nFile is open");

        printf("\n\n\nEnter Vehicle Registration Number: ");
        scanf("%s", & CustomerFile.vehicleRegNumber);

        while (CustomerFile.vehicleRegNumber != 0) /*#IF THE USER does not enter 0 the loops should begin, but there is a problem here*/
        {

            printf("\nFirst Name: ");
            fscanf("%s", CustomerFile.driverfName); /*here is the problem, I think is had something to do with the struct name*/
            printf("\nLast Name:  ");
            printf("\nType of Driver:  ");
            printf("\nDriver's License Number:  ");
            printf("\nVehicle Make:  ");
            printf("\nVehicle Model:   ");
            printf("\nComments   ");
            printf("\nParking SpaceTaken ");

            printf("\n\nenter firstname, lastname");
            fscanf(stdin, "%s%s%s%s%s%s%s%s1f", CustomerFile.driverfName I think this has something to do with the statement * /
                                CustomerFile.driverlName   / * okay here * /
                                CustomerFile.typeOfDriver       / * okay here * /
                                CustomerFile.driverLicNumber         / * okay here * /
                                CustomerFile.vehicleMake      / * okay here * /
                                CustomerFile.vehicleModel       / * okay here * /
                                CustomerFile.Comments       / * okay here * /
                                &CustomerFile.Lot_taken);       / * okay here * /


                        fwrite( sizeof(struct CustomerFile ), 1, cfPtr);




                }
                        fclose( cfPtr);
                }


return 0;

}

Okay the problem is that it keeps giving the errors;*

File.cpp:144: error: expected primary-expression before ‘.’ token

File.cpp:148: error: expected primary-expression before ‘.’ token

File.cpp:162: error: expected primary-expression before ‘.’ token

File.cpp:172: error: invalid conversion from ‘unsigned int’ to ‘const void*’

File.cpp:172: error: invalid conversion from ‘FILE*’ to ‘size_t’ /usr/include/stdio.h:708: error: too few arguments to function ‘size_t fwrite(const void*, size_t, size_t, FILE*)’ File.cpp:172: error: at this point in file

I believed or read that it has something with the fact that a C++ complier does not work with c99. If so, then how do you use structs in c++? I know you use a struct by just for example CustomerFile.driverlName, however, the complier keep refusing it. Also I'm having problems with the while loop. I'm familiar with c and c++ we were taught both c and c++, the code is to be written in c++ but the text book gives c code that won't run in a c++ complier.

Upvotes: 0

Views: 1672

Answers (2)

999k
999k

Reputation: 6565

You defined a datatype CustomerFile. For using defined structure CustomerFile you have create an object and use it. For eg :

CustomerFile  customer; 
customer.vehicleModel = "ABC"; 

vehicleRegNumber is of type string not integer compare it with 0 like this

while (customer.vehicleRegNumber != "0" )

Add , between variable names

  fscanf( stdin, "%s%s%s%s%s%s%s%s1f", customer.driverfName, customer.driverlName ,

The fscanf() function is a C function, it does not know about std::string (or classes). So you have use a temp c string like this

      char temp[100];
      printf("\nFirst Name: ");
      fscanf(stdin, "%99s", temp );
      customer.driverfName = temp;

Upvotes: 1

David G
David G

Reputation: 96800

CustomerFile is a class, so it won't work when you try to access data members off of it as if it were an instance. To create an instance, do:

CustomerFile file;

And replace all instances of Customer. with file. and it should resolve the error.

Upvotes: 2

Related Questions