我正在尝试为链接列表的成员创建一个搜索栏。不过,目前,我尝试过的任何方法都无法正常工作。
数据输入、搜索栏和数据输出栏需要是单独的功能。这些是我目前的原型:
item* Data_Input(void);
void titlesrch_bar( item*, int);
void Data_Output(string BookTitle, string ISBN, string Author, string EXPenroll, string price, string reqNreq, string UsedUnused);
我当前的数据输入函数是:
item* Data_Input(void)
{
item* new_item = new item;
cout << " Data Input Screen" << endl;
cout << endl;
cout << " Enter The Requested Data For Each Entry, Then Press <Enter> To Continue" << endl;
cout << " Book Title: ";
getline(cin, new_item->BookTitle);
cout << " Book ISBN: ";
getline(cin, new_item->ISBN);
cout << " Book Author: ";
getline(cin, new_item->Author);
cout << " Expected Enrollment: ";
getline(cin, new_item->EXPenroll);
while ((new_item->EXPenroll.at(0) < '0') || (new_item->EXPenroll.at(0) > '9'))
{
Error_Screen();
getline(cin, new_item->EXPenroll);
}
cout << " Book Cost(Price): $";
getline(cin, new_item->price);
while((new_item->price.at(0) < '0') || (new_item->price.at(0) > '9')) // Book price error trap
{
Error_Screen();
getline(cin, new_item->price);
}
cout << " Is this book Required (Y/N): ";
getline (cin, new_item->reqNreq);
cout << " Is this book Unused (Y/N): " ;
getline (cin, new_item->UsedUnused);
return new_item;
}
我现在的数据输出函数是这样的:
void Data_Output(string BookTitle, string ISBN, string Author, string EXPenroll, string price, string reqNreq, string UsedUnused)
{
{
int enroll = 0;
double OrderFactor = 0.0;
double Bprice = 0.0; // value conversions
double tot_value = 0.0;
int BookOrder = 0;
string resp = "XXX";
cout << " Book Title:" << BookTitle << endl;
cout << " Book ISBN: " << ISBN << endl;
cout << " Book Author: " << Author << endl;
cout << " Expected Enrollment: " << EXPenroll << endl;
cout << " Book Cost (Price): $" << price << endl;
cout << " Is this book Required (Y/N): " << reqNreq << endl;
cout << " Is this book Unused (Y/N): " << UsedUnused << endl;
if((reqNreq == "Y") || (reqNreq == "y"))
{
if((UsedUnused == "Y") || (UsedUnused == "y"))
{
OrderFactor = 1.0; // Book is new and required
}
else
{
OrderFactor = 0.60; // Book is used and required
}
}
else // Order factor calculation
{
if((UsedUnused == "Y") || (UsedUnused == "y"))
{
OrderFactor = 0.35; // Book is new and not required
}
else
{
OrderFactor = 0.10; // Book is used and not required
}
}
enroll = atoi(EXPenroll.data());
BookOrder = OrderFactor * enroll;
cout << " Books needed to be ordered: " << BookOrder << endl; // number of books to be ordered calculation
Bprice = atof(price.data());
tot_value = Bprice * BookOrder; // total inventory value calculation
cout << " Total Order Value: $" ;
std::cout << std::setprecision(8) << tot_value << std::endl;
}
}
到目前为止,我已经尝试了多种搜索栏。我目前正在使用这样的
for
循环:
void titlesrch_bar(item* order, int num_entry)
{
item* current = order;
string title = "XXX";
int numFound = 0;
cout << " Enter a Title to locate: ";
getline(cin, title);
for (; current != nullptr; current = current->next)
{
if (current->BookTitle == title)
{
Data_Output(current->BookTitle, current->ISBN, current->Author, current->EXPenroll, current->price, current->reqNreq, current->UsedUnused);
numFound++;
}
}
if(numFound == 0)
{
cout << "Not found" << endl;
}
}
然而,它调用输出函数失败,我不完全确定为什么。我还尝试了一个
while
循环,但是我用来打破循环的每一种方法都会导致搜索栏无法调用输出函数。唯一有效的方法是一个没有中断的 while
循环,像这样:
void titlesrch_bar(item* order, int num_entry)
{
item*current = order;
string title = "XXX";
cout << " Enter a ISBN to locate: ";
getline(cin, title);
while (current != nullptr)
{
if (current->BookTitle == title)
{
Data_Output(current->BookTitle, current->ISBN, current->Author, current->EXPenroll, current->price, current->reqNreq, current->UsedUnused);
}
}
}
这当然会导致无限循环,但是当我试图打破它时,搜索函数也无法调用输出函数。
我不确定我的确切错误是什么,或者是否有更好的方法来搜索列表成员。