2012-03-24 84 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()环路。我没有看到任何测试阻止使用超过数组(硬编码)大小的无效索引。