为什么我不能打印出我的字符串数组 C++?
Why can't I print out my string array c++?
我已经编写了这段代码,我应该读取一个 txt 文件并将 txt 文件中的每隔一行读取到字符串数组 bookTitle[ARRAY_SIZE] 并将其他每隔一行读取到 bookAuthor[ ARRAY_SIZE]。这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int ARRAY_SIZE = 1000;
string bookTitle [ARRAY_SIZE];
string bookAuthor [ARRAY_SIZE];
int loadData(string pathname);
void showAll(int count);
//int showBooksByAuthor (int count, string name);
//int showBooksByTitle (int count, string title);
int main ()
{
int number, numOfBooks;
char reply;
string bookTitles, authorName, backupFile;
cout << "Welcome to Brigham's library database." << endl;
cout << "Please enter the name of the backup file:";
cin >> backupFile;
numOfBooks = loadData (backupFile);
if (numOfBooks == -1) {
cout << endl;
} else {
cout << numOfBooks << " books loaded successfully." << endl;
}
cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All:";
cin >> reply;
do {
switch (reply) {
case 'a':
case 'A':
cout << "Author's name: ";
cin >> authorName;
showBooksByAuthor (numOfBooks, authorName);
cout << endl;
break;
case 'q':
case 'Q':
cout << endl;
break;
case 's':
case 'S':
showAll(numOfBooks);
break;
case 't':
case 'T':
cout << "Book title: ";
cin >> bookTitles;
showBooksByTitle(numOfBooks, bookTitles);
cout << endl;
break;
default:
cout << "Invalid input" << endl;
break;
}
} while (reply != 'q' && reply != 'Q');
while (1==1) {
cin >> number;
cout << bookTitle[number] << endl;
cout << bookAuthor[number] << endl;
}
}
int loadData (string pathname){
int count = 0, noCount = -1;
ifstream inputFile;
string firstLine, secondLine;
inputFile.open(pathname.c_str());
if (!inputFile.is_open()) { //If the file does not open then print error message
cout << "Unable to open input file." << endl;
return noCount;
}
for (int i = 0; i <= ARRAY_SIZE; i++) {
while (!inputFile.eof()) {
getline(inputFile, firstLine);
bookTitle[i] = firstLine;
getline(inputFile, secondLine);
bookAuthor[i] = secondLine;
cout << bookTitle[i] << endl;
cout << bookAuthor[i] << endl;
count++;
}
}
return count;
}
void showAll (int count) {
for (int j = 0; j <= count; j++) {
cout << bookTitle[j] << endl;
cout << bookAuthor[j] << endl;
}
}
所以我有 loadData 函数,我很确定这是我的问题。当我让它打印出每个字符串 [ith position] 而 运行 加载数据函数时,它会打印出每个标题和作者,就像它出现在 txt 文件中一样。但是当我 运行 应该能够将整个 txt 文档打印到屏幕上的 void showAll 函数不起作用时。另外,我只是检查了字符串是否实际存储在内存中,而实际上没有。 (在我的 do while 循环之后,我有一个 while 循环,它接受 int 类型的输入,然后打印 [input position] 的字符串数组。这什么都不打印。那么我必须做什么才能将每一行实际存储到不同的位置在字符串数组中?请随意更正我的代码,但考虑到我还有两个函数我还没有做任何事情,它还不够好。(已注释掉)。
您的主要问题是您尝试使用 两个 循环而不是一个循环来读取数据!您想要读取直到输入失败或数组已满,即类似这样的内容:
for (int i = 0;
i < ARRAY_SIZE
&& std::getline(inputFile, bookTitle[i])
&& std::getline(inputFile, bookAuthor[i]); ++i) {
}
原始代码的问题在于它从不更改索引 i
,并且总是将值存储到索引为 0
的单元格中。由于在读取输入 之后 未检查输入,因此最后一次循环迭代无法读取某些内容并用空值覆盖任何先前存储的值。一旦读取流失败,外循环将遍历所有索引但不执行任何操作,因为对内循环的检查始终是 false
.
我已经编写了这段代码,我应该读取一个 txt 文件并将 txt 文件中的每隔一行读取到字符串数组 bookTitle[ARRAY_SIZE] 并将其他每隔一行读取到 bookAuthor[ ARRAY_SIZE]。这是我的代码:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int ARRAY_SIZE = 1000;
string bookTitle [ARRAY_SIZE];
string bookAuthor [ARRAY_SIZE];
int loadData(string pathname);
void showAll(int count);
//int showBooksByAuthor (int count, string name);
//int showBooksByTitle (int count, string title);
int main ()
{
int number, numOfBooks;
char reply;
string bookTitles, authorName, backupFile;
cout << "Welcome to Brigham's library database." << endl;
cout << "Please enter the name of the backup file:";
cin >> backupFile;
numOfBooks = loadData (backupFile);
if (numOfBooks == -1) {
cout << endl;
} else {
cout << numOfBooks << " books loaded successfully." << endl;
}
cout << "Enter Q to (Q)uit, Search (A)uthor, Search (T)itle, (S)how All:";
cin >> reply;
do {
switch (reply) {
case 'a':
case 'A':
cout << "Author's name: ";
cin >> authorName;
showBooksByAuthor (numOfBooks, authorName);
cout << endl;
break;
case 'q':
case 'Q':
cout << endl;
break;
case 's':
case 'S':
showAll(numOfBooks);
break;
case 't':
case 'T':
cout << "Book title: ";
cin >> bookTitles;
showBooksByTitle(numOfBooks, bookTitles);
cout << endl;
break;
default:
cout << "Invalid input" << endl;
break;
}
} while (reply != 'q' && reply != 'Q');
while (1==1) {
cin >> number;
cout << bookTitle[number] << endl;
cout << bookAuthor[number] << endl;
}
}
int loadData (string pathname){
int count = 0, noCount = -1;
ifstream inputFile;
string firstLine, secondLine;
inputFile.open(pathname.c_str());
if (!inputFile.is_open()) { //If the file does not open then print error message
cout << "Unable to open input file." << endl;
return noCount;
}
for (int i = 0; i <= ARRAY_SIZE; i++) {
while (!inputFile.eof()) {
getline(inputFile, firstLine);
bookTitle[i] = firstLine;
getline(inputFile, secondLine);
bookAuthor[i] = secondLine;
cout << bookTitle[i] << endl;
cout << bookAuthor[i] << endl;
count++;
}
}
return count;
}
void showAll (int count) {
for (int j = 0; j <= count; j++) {
cout << bookTitle[j] << endl;
cout << bookAuthor[j] << endl;
}
}
所以我有 loadData 函数,我很确定这是我的问题。当我让它打印出每个字符串 [ith position] 而 运行 加载数据函数时,它会打印出每个标题和作者,就像它出现在 txt 文件中一样。但是当我 运行 应该能够将整个 txt 文档打印到屏幕上的 void showAll 函数不起作用时。另外,我只是检查了字符串是否实际存储在内存中,而实际上没有。 (在我的 do while 循环之后,我有一个 while 循环,它接受 int 类型的输入,然后打印 [input position] 的字符串数组。这什么都不打印。那么我必须做什么才能将每一行实际存储到不同的位置在字符串数组中?请随意更正我的代码,但考虑到我还有两个函数我还没有做任何事情,它还不够好。(已注释掉)。
您的主要问题是您尝试使用 两个 循环而不是一个循环来读取数据!您想要读取直到输入失败或数组已满,即类似这样的内容:
for (int i = 0;
i < ARRAY_SIZE
&& std::getline(inputFile, bookTitle[i])
&& std::getline(inputFile, bookAuthor[i]); ++i) {
}
原始代码的问题在于它从不更改索引 i
,并且总是将值存储到索引为 0
的单元格中。由于在读取输入 之后 未检查输入,因此最后一次循环迭代无法读取某些内容并用空值覆盖任何先前存储的值。一旦读取流失败,外循环将遍历所有索引但不执行任何操作,因为对内循环的检查始终是 false
.