0

我正在努力完成这段代码。

#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;

}

好的,问题是它不断给出错误;*

File.cpp:144:错误:“。”之前的预期主表达式 令牌

File.cpp:148:错误:“。”之前的预期主表达式 令牌

File.cpp:162:错误:“。”之前的预期主表达式 令牌

File.cpp:172:错误:从 'unsigned int' 到 'const void*' 的无效转换</p>

File.cpp:172: 错误: 从 'FILE*' 到 'size_t' /usr/include/stdio.h:708: 错误: 函数'size_t fwrite(const void*, size_t, size_t, FILE)的参数太少*)' File.cpp:172: 错误:此时在文件中

我相信或读到它与 C++ 编译器不适用于 c99 的事实有关。如果是这样,那么您如何在 C++ 中使用结构?我知道您仅通过 CustomerFile.driverlName 使用结构,但是,编译器一直拒绝它。我也遇到了while循环的问题。我熟悉 c 和 c++,我们同时学习了 c 和 c++,代码要用 c++ 编写,但教科书提供的 c 代码不能在 c++ 编译器中运行。

4

2 回答 2

2

CustomerFile是一个类,因此当您尝试访问它的数据成员时,它将不起作用,就好像它是一个实例一样。要创建实例,请执行以下操作:

CustomerFile file;

Customer.并替换with的所有实例,file.它应该可以解决错误。

于 2013-04-01T01:48:51.157 回答
1

您定义了一个数据类型CustomerFile。要使用定义的结构CustomerFile,您必须创建一个对象并使用它。例如:

CustomerFile  customer; 
customer.vehicleModel = "ABC"; 

vehicleRegNumber是字符串类型而不是整数类型比较它0像这样

while (customer.vehicleRegNumber != "0" )

,在变量名之间添加

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

fscanf()函数是一个 C 函数,它不知道std::string(或类)。所以你使用了一个像这样的临时 c 字符串

      char temp[100];
      printf("\nFirst Name: ");
      fscanf(stdin, "%99s", temp );
      customer.driverfName = temp;
于 2013-04-01T01:54:42.553 回答