我想今晚完成我的最终调试。我的问题是我已经编写了这两天的代码,它有一些问题。 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];
         }
     }

}

最佳答案

逗号运算符应分别替换为逻辑andor&&||。请参见uses of the comma operator。此外,authorSearchvoid函数。如果要调用authorSearch,只需编写authorSearch(...)而不是int authorSearch(...)

另外,您需要确保原型与实现保持一致。 int authorSearch (string bookAuthor [50])void authorSearch (string bookAuthor [50], char choice)不同。您不匹配它们的类型和参数。

关于c++ - 数组及其搜索,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/9848821/

10-09 04:38