简体   繁体   English

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

[英]Why can't I print out my string array c++?

I have written this code and I am supposed to read in a txt file and read every other line in the txt file to the string array bookTitle[ARRAY_SIZE] and the other every other line to bookAuthor[ARRAY_SIZE]. 我已经编写了这段代码,应该读取一个txt文件并将该txt文件中的每一行都读取到字符串数组bookTitle [ARRAY_SIZE]中,另一行读取到bookAuthor [ARRAY_SIZE]。 Here is my code: 这是我的代码:

#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;
    }
}

So I have the loadData function which I am pretty sure is my problem. 所以我有loadData函数,我很确定这是我的问题。 When I have it print out each string[ith position] while running the loadData function it prints out each title and author just as it appears in the txt file. 当我在运行loadData函数的同时打印出每个字符串[第一个位置]时,它会打印出每个标题和作者,就像在txt文件中一样。 But then when I run the void showAll function which is supposed to be able to print the entire txt doc to the screen it doesn't work. 但是,当我运行void showAll函数时,该函数应该能够将整个txt文档打印到屏幕上,因此它不起作用。 Also just I checked to see if the strings were actually stored in memory and they were not. 同样,我检查了字符串是否确实存储在内存中,是否没有存储。 (After my do while loop I have a while loop that accepts input of type int and then prints the string array of the [input position]. This prints nothing. So what do I have to do to actually store each line to a different position in the string array(s)? Feel free to correct my code but it isn't pretty yet considering I still have two functions that I haven't done anything too. (Commented out). (在执行do while循环之后,我有一个while循环,该循环接受int类型的输入,然后打印[input position]的字符串数组。这不会打印任何内容。因此,我该怎么做才能将每​​一行实际存储到不同的位置可以随意更正我的代码,但是考虑到我还有两个函数,我还没有做任何事情,这还不是很漂亮(注释掉)。

You main problem is that you try to read you data using two loops rather than just one! 您的主要问题是您尝试使用两个循环而不只是一个循环读取数据! You want read until either input fails or the array is filled, ie, something like this: 您要读取直到输入失败或数组被填充为止,即类似以下内容:

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

The problem with the original code is that it never changes the index i and always stores values into the cell with index 0 . 原始代码的问题在于,它永远不会更改索引i并且始终将值存储到索引为0的单元格中。 Since the input isn't checked after it is being read, the last loop iteration fails to read something and overwrites any earlier stored value with an empty value. 由于在读取输入未对其进行检查,因此最后一个循环迭代无法读取内容,并使用空值覆盖任何先前存储的值。 Once reading of the stream fails the outer loop iterates over all indices but doesn't do anything as the check to the inner loop is always false . 一旦读取流失败,则外循环将遍历所有索引,但不会执行任何操作,因为对内循环的检查始终为false

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM