我很难完成这段代码。
#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:错误:在'。'标记
之前预期的primary-expressionFile.cpp:148:错误:在'。'标记
之前预期的primary-expressionFile.cpp:162:错误:在'。'标记
之前的预期primary-expressionFile.cpp:172:错误:无效转换为'unsigned int'到'const void *'
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 ++编译器中运行。
答案 0 :(得分:2)
CustomerFile
是一个类,因此当您尝试从其中访问数据成员时它将无法工作,就像它是实例一样。要创建实例,请执行:
CustomerFile file;
并将Customer.
的所有实例替换为file.
,它应该可以解决错误。
答案 1 :(得分:1)
您定义了数据类型CustomerFile
。要使用已定义的结构CustomerFile
,您需要创建一个对象并使用它。例如:
CustomerFile customer;
customer.vehicleModel = "ABC";
vehicleRegNumber
的类型为字符串而不是整数,将其与此{/ 1>} 0
进行比较
while (customer.vehicleRegNumber != "0" )
在变量名之间添加,
fscanf( stdin, "%s%s%s%s%s%s%s%s1f", customer.driverfName, customer.driverlName ,
fscanf()
函数是一个C函数,它不知道std::string
(或类)。所以你使用了像这样的临时字符串
char temp[100];
printf("\nFirst Name: ");
fscanf(stdin, "%99s", temp );
customer.driverfName = temp;