首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >错误c2109:下标要求数组或指针类型为5

错误c2109:下标要求数组或指针类型为5
EN

Stack Overflow用户
提问于 2014-12-05 06:55:45
回答 1查看 3.5K关注 0票数 0

我遇到了一个关于for循环和数组的小问题,我希望我能得到一些帮助。在第一个函数中,使用for循环来调用该函数的每个值很好。但是,在第二个函数中,我从Visual中得到一个错误,说明“下标需要数组或指针类型”。我在这里做错了什么,导致了这个错误?

这个程序的目的是搜索txt文件中的书籍,跳过条目之间的行,找出文件中匹配的条目数量和它们在哪里,并打印出每个条目的详细信息。

代码语言:javascript
复制
void bookSearch(string id) {
    ifstream fbooks;

    string item = " ", entry = " ";

    int resultLocation[30];

    int searchType = 0;

    fbooks.open("books.txt");

    cout << "Welcome to the Book Search System, " << id << ".\n"
        << "What do you wish to search the registry by?\n"
        << "1. ISBN Number\n" << "2. Author\n" << "3. Title\n";

    while (searchType<1 || searchType>3) {
        cin >> searchType;

        if (searchType<1 || searchType>3) {
            displayMessage(0);

        }
    }

    getline(cin, item);

    for (int x = 0; x <= 30; x++) {
        for (int y = 0; y < searchType; y++)
            getline(fbooks, entry);

        if (entry == item)
            resultLocation[x] = 1;
        else
            resultLocation[x] = 0;

        for (int z = 0; z < 5; z++)
            getline(fbooks, entry);
    }

    resultPrint(resultLocation, id);
}


void resultPrint(int resultLocation, string id){
    int resultNum = 0;

    string entry = "";

    ifstream fbooks;

    fbooks.open("books.txt");

    for (int a = 0; a <= 30; a++) {
        if (resultLocation == 1)
            resultNum++;
    }

    if (resultNum > 0) {
        cout << endl << "There are " << resultNum << " entries in the database matching that criteria.\n";

        for (int a = 0; a <= 30; a++){
            if (resultLocation[a] == 1) { //The a in this line is marked with the error
                for (int b = 0; b <= 2; b++) {
                    getline(fbooks, entry);
                    cout << entry;
                }
            }
        }
    }

    else
        cout << endl << "There are no entries in the database matching that criteria.\n";
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-05 07:16:45

resultLocation是一个整数,所以您不能在它上使用operator[] (除了第一个“索引”)。看起来你是想让它成为一个数组:

代码语言:javascript
复制
void resultPrint(int resultLocation[], string id);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27310486

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档