为什么我不能打印出我的字符串数组c ++?

时间:2015-11-28 18:26:13

标签: c++ arrays string function fstream

我已经编写了这段代码,我应该读取一个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函数,我很确定这是我的问题。当我在运行loadData函数时打印出每个字符串[ith position]时,它会打印出每个标题和作者,就像它出现在txt文件中一样。但是当我运行void showAll函数时,它应该能够将整个txt doc打印到屏幕上,但是它不起作用。另外我只是检查字符串是否实际存储在内存中而它们不是。 (在我执行while循环之后,我有一个接受int类型输入的while循环,然后打印[输入位置]的字符串数组。这没有打印。所以我需要做什么才能将每一行实际存储到不同的位置在字符串数组中?可以随意更正我的代码,但它还没有考虑到我还有两个函数,我还没有做过任何事情。(注释掉)。

1 个答案:

答案 0 :(得分:1)

您的主要问题是,您尝试使用两个循环而不是一个循环来读取数据!你想要读取,直到输入失败或数组被填充,即,像这样:

for (int i = 0;
     i < ARRAY_SIZE
     && std::getline(inputFile, bookTitle[i])
     && std::getline(inputFile, bookAuthor[i]); ++i) {
}

原始代码的问题在于它永远不会更改索引i并始终将值存储到索引为0的单元格中。由于在之后未检查输入,因此最后一次循环迭代无法读取内容并使用空值覆盖任何先前存储的值。一旦读取流失败,外部循环遍历所有索引但不执行任何操作,因为对内部循环的检查始终为false