2012-03-24 61 views
0

我想完成我今晚的最終調試。我的問題是,我一直在寫這個代碼幾天,它有一些問題。 Previous Post數組和搜索他們

它現在編譯並不會崩潰,但有一些問題與我的功能不正常工作(或根本)。

#include <iostream>    
#include <string> 
#include <fstream> 
using namespace std; 

string bookTitle [50]; 
string bookAuthor [50]; 
int loadData (string pathname);   
int showall (int counter); 
int authorSearch (string bookAuthor [50]); 




int main() 

{ 
    string pathname; 
    int counter=0; 
    char choice; 

    cout<<"Input the name of the file to be accessed: "; 
    cin>>pathname; 
    loadData (pathname); 
    showall (counter); 

    cout<<"\n\n\n\n What would you like to do \n (A for Author Search , T for Title Search, Q to quit):"; 
    cin>>choice; 

    while (choice != 'Q' , choice != 'q') 
    { 
      if (choice == 'A', choice == 'a') 
      { 
        int authorSearch (string bookAuthor [50], char choice); 
      } 

      if (choice == 'T', choice == 't') 
      { 
        int titleSearch (string bookTitle [50], char choice); 
      } 


    } 

    cout<<"Press <Enter> to Exit"; 
    cin.ignore(); 
    cin.get();  
    return 0;    

    cout<<"Press <Enter> to Exit"; 
    cin.ignore(); 
    cin.get();  
    return 0;     
} 


int loadData (string pathname) // Loads data from infile into arrays 
{ 
    fstream infile; 
    int counter = 0; 
    infile.open(pathname.c_str()); //Opens file from user input in main 
    if(infile.fail()) 
    { 
     cout << "File failed to open"; 
     return 0; 
    } 

    while (!infile.eof()) 
    { 

      infile >> bookTitle [counter] ; //takes input and puts into parallel arrays 
      infile >> bookAuthor [counter]; 
      counter++; 
    } 

    infile.close(); 
} 

int showall (int counter)  // shows input in title(author) format 
{ 

    cout<<bookTitle<<"("<<bookAuthor<<")"; 

} 

void authorSearch (string bookAuthor [50], char choice) // Function to search Author Array 
{ 
    string target = ""; 
    cout<<"Which author would you like to search for: "<<target; //input 
    for (int count = 0; count++;) 
    { 
     if(bookAuthor[count] == target) //tests input against array and outputs result 
     { 
           cout<<bookTitle[count]<<bookAuthor[count]; 
     } 
    } 

} 



void titleSearch (string bookTitle [50], char choice) // Function to Serch Title Array 
{ 
    string target = ""; 
    cout<<"Which author would you like to search for: "<<target; //input 
    for (int count = 0; count++;) 
    { 
     if(bookAuthor[count] == target) //tests input against array and outputs result 
     { 
           cout<<bookTitle[count]<<bookAuthor[count]; 
     } 
    } 

} 

最新版本,沒有重大改進。菜單選擇後,我無法使功能正常工作。 ShowAll似乎工作,但輸出十六進制。再次感謝大家!

#include <iostream>    
#include <string> 
#include <fstream> 
using namespace std; 

string bookTitle [50]; 
string bookAuthor [50]; 
int loadData (string pathname);   
int showall (int counter); 
void authorSearch (string bookAuthor [50]); 
void titleSearch (string bookTitle [50]); 



int main() 

{ 
    string pathname; 
    int counter=0; 
    char choice; 

    cout<<"Input the name of the file to be accessed: "; 
    cin>>pathname; 
    loadData (pathname); 
    showall (counter); 

    cout<<"\n\n\n\n What would you like to do \n (A for Author Search , T for Title Search, Q to quit):"; 
    cin>>choice; 

    while (choice != 'Q'|| choice != 'q') 
    { 
      if (choice == 'A'|| choice == 'a') 
      { 
        void authorSearch (string bookAuthor [50], char choice); 
      } 

      if (choice == 'T'|| choice == 't') 
      { 
        void titleSearch (string bookTitle [50], char choice); 
      } 


    } 

    cout<<"Press <Enter> to Exit"; 
    cin.ignore(); 
    cin.get();  
    return 0;    

} 


int loadData (string pathname) // Loads data from infile into arrays 
{ 
    fstream infile; 
    int counter = 0; 
    infile.open(pathname.c_str()); //Opens file from user input in main 
    if(infile.fail()) 
    { 
     cout << "File failed to open"; 
     return 0; 
    } 

    while (!infile.eof()) 
    { 

      infile >> bookTitle [counter] ; //takes input and puts into parallel arrays 
      infile >> bookAuthor [counter]; 
      counter++; 
    } 

    infile.close(); 
} 

int showall (int counter)  // shows input in title(author) format 
{ 

    cout<<bookTitle<<"("<<bookAuthor<<")"; 

} 

void authorSearch (string bookAuthor [50], char choice) // Function to search Author Array 
{ 
    string target = ""; 
    cout<<"Which author would you like to search for: "<<target; //input 
    for (int count = 0; count++;) 
    { 
     if(bookAuthor[count] == target) 
     { 
           cout<<bookTitle[count]<<bookAuthor[count]; 
     } 
    } 

} 



void titleSearch (string bookTitle [50], char choice) // Function to Serch Title Array 
{ 
    string target = ""; 
    cout<<"Which title would you like to search for: "<<target; //input 
    for (int count = 0; count++;) 
    { 
     if(bookAuthor[count] == target) //tests input against array and outputs reults 
     { 
           cout<<bookTitle[count]<<bookAuthor[count]; 
     } 
    } 

} 
+0

您可能希望使用逗號運算符更加明確。使用&&或||代替。 – collinjsimpson 2012-03-24 03:12:59

+0

請將此標記爲家庭作業,我知道您之前的帖子提到了它,但這不是。 此外,提示:如果您選擇了錯誤的選擇,我該如何再次選擇? – 2012-03-24 03:13:46

+0

感謝將此標記爲HW – kd7vdb 2012-03-24 03:24:42

回答

2

逗號操作員應分別與邏輯andor&&||代替。見uses of the comma operator。另外,authorSearchvoid函數。如果你想撥打authorSearch,簡單的寫authorSearch(...)而不是int authorSearch(...)

此外,您需要確保您的原型與您的實現一致。 int authorSearch (string bookAuthor [50])void authorSearch (string bookAuthor [50], char choice)不一樣。您的類型與他們的參數不匹配。

+1

只要指出'&&'和'||'是邏輯的'和'和'或'。按位是'&'和'|'。 – chris 2012-03-24 03:52:27

+0

我的錯誤。謝謝! – collinjsimpson 2012-03-24 03:53:43

+0

如果我擺脫了authorsearch前面的類型說明符,它將不會編譯。 – kd7vdb 2012-03-24 04:01:35

0

1)showall()函數輸出十六進制,因爲你不能以這種方式顯示數組,你需要某種循環。它只是打印每個陣列的起始地址

2)在您的搜索功能中,您永遠不會從用戶處讀取target字符串。

3)這些for()循環永遠不會執行:

for (int count = 0; count++;) 
{ 
    ... 
} 

您設置count0,然後遞增之前測試值。測試失敗,循環體不執行。

4)小心修復for()環路。我沒有看到任何測試阻止使用超過數組(硬編碼)大小的無效索引。