2014-12-05 286 views
0

我一直在遇到有關for循環和數組的小問題,我希望能得到一些幫助。在第一個函數中,使用for循環調用該函數的每個值都可以工作。但是,在第二個函數中,我從Visual Studio中得到一個錯誤,指出「下標需要數組或指針類型」。我在這裏做錯了什麼導致這個錯誤?錯誤c2109:下標需要數組或指針類型5

此程序的目的是搜索書籍的txt文件,跳過條目之間的行,找出文件中有多少條目匹配以及它們在哪裏,並打印出每個條目的詳細信息。

void bookSearch(string id) { 
    ifstream fbooks; 

    string item = " ", entry = " "; 

    int resultLocation[30]; 

    int searchType = 0; 

    fbooks.open("books.txt"); 

    cout << "Welcome to the Book Search System, " << id << ".\n" 
     << "What do you wish to search the registry by?\n" 
     << "1. ISBN Number\n" << "2. Author\n" << "3. Title\n"; 

    while (searchType<1 || searchType>3) { 
     cin >> searchType; 

     if (searchType<1 || searchType>3) { 
      displayMessage(0); 

     } 
    } 

    getline(cin, item); 

    for (int x = 0; x <= 30; x++) { 
     for (int y = 0; y < searchType; y++) 
      getline(fbooks, entry); 

     if (entry == item) 
      resultLocation[x] = 1; 
     else 
      resultLocation[x] = 0; 

     for (int z = 0; z < 5; z++) 
      getline(fbooks, entry); 
    } 

    resultPrint(resultLocation, id); 
} 


void resultPrint(int resultLocation, string id){ 
    int resultNum = 0; 

    string entry = ""; 

    ifstream fbooks; 

    fbooks.open("books.txt"); 

    for (int a = 0; a <= 30; a++) { 
     if (resultLocation == 1) 
      resultNum++; 
    } 

    if (resultNum > 0) { 
     cout << endl << "There are " << resultNum << " entries in the database matching that criteria.\n"; 

     for (int a = 0; a <= 30; a++){ 
      if (resultLocation[a] == 1) { //The a in this line is marked with the error 
       for (int b = 0; b <= 2; b++) { 
        getline(fbooks, entry); 
        cout << entry; 
       } 
      } 
     } 
    } 

    else 
     cout << endl << "There are no entries in the database matching that criteria.\n"; 
} 
+0

'無效resultPrint(INT */* * << 2014-12-05 07:02:18

+0

並且還'如果(resultLocation [一] == 1)' 。 – 2014-12-05 07:05:44

回答

0

resultLocation是一個整數,所以你不能用它operator[](爲除第一個「指數」)。看起來像你的意思是讓一個數組:

void resultPrint(int resultLocation[], string id);