2013-04-01 24 views
0

我正在努力完成此代碼。File.cpp:148:錯誤:在'。'之前預期的主表達式令牌不同符號

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

我認爲還是看它有這樣一個事實:C++編譯器不與C99一起想。如果是這樣,那麼你如何在C++中使用結構?我知道你只是使用一個結構,例如CustomerFile.driverlName,但是,編譯器不斷拒絕它。此外,我遇到了while循環的問題。我熟悉c和C++,我們都被教過c和C++,代碼是用C++編寫的,但是教科書給出的c代碼不會在C++編譯器中運行。

回答

2

CustomerFile是一個類,所以當您嘗試訪問數據成員時它不起作用,就好像它是一個實例。要創建一個實例,這樣做:

CustomerFile file; 

而且隨着file.取代的Customer.所有實例,它應該解決的錯誤。

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; 
+0

File.cpp:170:warning:不能通過'...'傳遞非POD類型'struct std :: string'的對象;調用將在運行時中止。是我添加逗號時得到的。 –

+0

這就是我提到的最後一點的錯誤。 fscanf()不知道std :: string。所以你必須使用臨時c字符串fscanf(stdin,「%s%s%s%s%s%s%s%s1f」 – 999k

相關問題