如何知道我想在数组中存储多少索引
How to know how many index i want to store in the arrays
我想编写一个搜索函数,您可以在其中从文件中获取字符串。该函数要求用户输入要搜索的字符串的第一个字母,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++中的稳定内存中
- std::原子加载和存储都需要吗
- C++:将控制台输出存储在宏中更好吗
- 复制列表初始化的隐式转换的等级是多少
- while循环中while循环的时间复杂度是多少
- 使用QProcess执行命令,并将结果存储在QStringList中
- 访问存储在向量C++中的结构的多态成员
- 如何从存储在std::映射中的std::集中删除元素
- 存储模板类型以强制转换回派生<T>
- 类型总是使用其大小存储在内存中吗
- 在 Linux 中存储区域设置名称的缓冲区大小应该是多少?
- 在二维向量或数组中可以存储的最大元素数是多少?
- 存储指针值需要多少位
- 如何知道我想在数组中存储多少索引
- 何时有效使用存储桶排序以及需要多少存储桶
- 您可以在Arduino字符串中存储多少字节?
- 在C 中存储对对象的引用需要多少内存
- 使用犰狳线性代数包存储矩阵需要多少内存
- C++中存储在void中的值是多少
- ID3D11Buffer中可以存储的最大数据大小是多少?