我正在努力完成今晚的最后调试。我的问题是我已经写了几天这个代码,它有一些问题。 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 :(得分:2)
逗号运算符应分别替换为逻辑and
或or
,&&
和||
。见uses of the comma operator。此外,authorSearch
是void
函数。如果您想致电authorSearch
,请简单地写authorSearch(...)
而不是int authorSearch(...)
。
此外,您需要确保原型与您的实现一致。 int authorSearch (string bookAuthor [50])
与void authorSearch (string bookAuthor [50], char choice)
不同。您的类型和的参数不匹配。
答案 1 :(得分:0)
1)showall()
函数输出十六进制,因为你无法以这种方式显示数组,你需要某种循环。它只是打印每个数组的起始地址。
2)在搜索功能中,您从未阅读过用户的target
字符串。
3)这些for()
循环永远不会执行:
for (int count = 0; count++;)
{
...
}
您将count
设置为0
,然后在递增前测试该值。测试失败,循环体未执行。
4)修复for()
循环时要小心。我没有看到任何测试来阻止使用无效索引超过数组的(硬编码)大小。