gpt4 book ai didi

c++ - 如何知道我想在数组中存储多少个索引

转载 作者:行者123 更新时间:2023-11-28 05:08:37 24 4
gpt4 key购买 nike

我想编写一个搜索函数,您可以在其中从文件中找出字符串。该函数要求用户输入要搜索的字符串的首字母,for 循环将搜索首字母与用户请求相同的字符串。这是我的代码。

string bid, bname, bAuthor, bcat, bPub;
int bAmt = 0, i = 1;
bool bname2length;
char character[];
char firstChar;
ifstream inBook;
inBook.open("books.txt");

if(inBook.is_open())
{
cout << setfill('-') << setw(1) << "+" << setw(3) << "-" << setw(1) << "+" << setw(8) << "-" << setw(1) << "+" << setw(40) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(20) << "-" << setw(1) << "+" << setw(20) << "-" << setw(1) << "+" << setw(6) << "-" << setw(1) << "+" << endl;
cout << setfill(' ') << setw(1) << "|" << setw(3) << left << "No." << setw(1) << "|" << setw(8) << left << "BookID" << setw(1) << "|" << setw(40) << left << "Book Name" << setw(1) << "|" << setw(15) << left << "Author" << setw(1) << "|" << setw(20) << left << "Category" << setw(1) << "|" << setw(20) << left << "Publisher" << setw(1) << "|" << setw(6) << left << "Amount" << setw(1) << "|" << endl;
cout << setfill('-') << setw(1) << "+" << setw(3) << "-" << setw(1) << "+" << setw(8) << "-" << setw(1) << "+" << setw(40) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(20) << "-" << setw(1) << "+" << setw(20) << "-" << setw(1) << "+" << setw(6) << "-" << setw(1) << "+" << endl;

getline(inBook, bid, '#');
getline(inBook, bname, '#');
getline(inBook, bAuthor, '#');
getline(inBook, bAuthor, '#');
getline(inBook, bPub, '#');
inBook >> bAmt;
inBook.ignore(1);

while(!inBook.eof())
{
cout << setfill(' ') << setw(1) << "|" << setw(3) << left << i << setw(1) << "|" << setw(8) << left << bid << setw(1) << "|" << setw(40) << left << bname << setw(1) << "|" << setw(15) << left << bAuthor << setw(1) << "|" << setw(20) << left << bcat << setw(1) << "|" << setw(20) << left << bPub << setw(1) << "|" << setw(6) << left << bAmt << setw(1) << "|" << endl;
cout << setfill('-') << setw(1) << "+" << setw(3) << "-" << setw(1) << "+" << setw(8) << "-" << setw(1) << "+" << setw(40) << "-" << setw(1) << "+" << setw(15) << "-" << setw(1) << "+" << setw(20) << "-" << setw(1) << "+" << setw(20) << "-" << setw(1) << "+" << setw(6) << "-" << setw(1) << "+" << endl;

getline(inBook, bid, '#');
getline(inBook, bname, '#');
getline(inBook, bAuthor, '#');
getline(inBook, bcat, '#');
getline(inBook, bPub, '#');
inBook >> bAmt;
inBook.ignore(1);
i++;

cout << "Enter the First Character of Search Obj : ";
cin >> firstChar;

while(bid != ' ')
{
character[i] = bid;
i++;
}

cout << "Books with First Character of " + firstChar << endl;
for(int i=0; i<sizeof(character[]); i++)
{
if(character[i].chatAt(0) == firstChar)
{
cout << character[i] << endl;
}
}
}

inBook.close();
}

else
{
cout << "Invalid file!";
}

最佳答案

除了@NathanOliver 很棒的帖子,也不需要原始数组。你会惹上麻烦的。使用 std::vector 或类似的东西。

#include <vector>
std::vector<char> bids;

while(bid != ' ')
{
bids.push_back(bid);
}
cout << "Books with First Character of " + firstChar << endl;

// C++11 for loop..Otherwise loop with iterators.
for (const auto& abid : bids)
{
cout << bid << endl;
}

关于c++ - 如何知道我想在数组中存储多少个索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44003930/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com