0

I'm working on an assignment right now and when run my code returns this error:

main.cpp:60:20: error: ‘dataArr’ was not declared in this scope
         if(tolower(dataArr[i].last) == tolower(lastName))

I'm not quite sure what I'm missing here. If I could at least get it to run I'd appreciate it. Thanks. I thought arrays were declared globally so i thought it wouldn't be an issue in my functions

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
struct Database
{
    string first;
    string last;
    string ID;
    string phoneNum;
};
void lastSearch(string);
void idSearch(string);
int main()
{
    Database dataArr[100];
    ifstream myFile("library_database.txt");
    int count = 0;
    while(!myFile.eof() && count < 100)
    {
        myFile >> dataArr[count].first >> dataArr[count].last >> dataArr[count].ID >> dataArr[count].phoneNum;
        cout << dataArr[count].first << " " << dataArr[count].last << " " << dataArr[count].ID << " " << dataArr[count].phoneNum << endl;
        count++;
    }  
    int input;
    string search;
    cout << "Would you like to search by last name or member ID?\n1. Last Name\n2. ID\n> ";
    cin >> input;
    while(input != 1 || input != 2)
    {
        cout << "Enter a valid answer.\n> ";
        cin >> input;
    }
    if(input == 1)
    {
        cout << "Enter last name: ";
        cin >> search;
        lastSearch(search);
    }
    if(input == 2)
    {
        cout << "Enter ID: ";
        cin >> search;
        idSearch(search);
    }
    return 0;
}
void lastSearch(string lastName)
{
    int num = 0;
    for(int i = 0; i < 100; i++)
    {
        if(tolower(dataArr[i].last) == tolower(lastName))
        {
            cout << dataArr[i].first << " " << dataArr[i].last << " " << dataArr[i].ID << " " << dataArr[i].phoneNum << endl
            num++;
        }
    }
    if(num == 0)
    {
        cout << "No match was found in the file.";
    }
}

voidSearch was removed to allow this to be posted

  • 3
    Your array "dataArr" is not a global array, and is only an array declared inside the main() function. That is the reason the lastSearch() function does not know the "dataArr", which causes the compile error. – Job_September_2020 Mar 04 '22 at 23:59
  • You declare `dataArr` in function `int main()` so it can only be used in `int main()` - that is its scope. You probably need to pass it as a parameter to the functions you want to use it. – Galik Mar 05 '22 at 00:03
  • Nothing (I’m aware of) is declared implicitly as globally. – Taekahn Mar 05 '22 at 00:04
  • @Taekahn: I suspect, technically, an item declared at the top of a file (outside any function) could be considered implicit global. At least within the confines of the file, anyway :-) – paxdiablo Mar 05 '22 at 00:36

2 Answers2

0

To answer the title of your post: because it isn't.

You declare dataArr in main, but you are trying to use it in lastSearch, so lastSearch can't see it. But you can pass it in as a parameter, that's probably the easiest fix:

void lastSearch(const string lastName, const Database *dataArr) { ... }

and call it like this:

lastSearch (search, dataArr);

Note the use of const (get into the habit of doing that whenever you can) and that your array 'decays' to a pointer when you pass it as a parameter like this, so don't be tempted to use sizeof in lastSearch. If you need to know the number of elements in the array, pass that as a parameter too.

Or, better, use std::array instead of a C-style array and then the size of the array is available in lastSearch without the need to pass it in separately. If you do that, you probably want to pass it by const reference to avoid copying it every time you call the function.

Finally, it might be time to learn about std::vector. At the expense of a little more complexity (but not much), this would avoid the need to allocate a fixed size array. Again, for the same reason, pass it around by reference.


Some bedtime reading: The Definitive C++ Book Guide and List

Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • Well, I would say the *easiest* fix is to just move the declaration up two lines (before `main`), at least in terms of editor keystroke count :-) But, as per both our answers, `easy != right`. And vector is probably much better here since this arbitrary size of 100 is a bad idea. While OP collects the actual size, they still use `100` in the search function, though they may have read far fewer. – paxdiablo Mar 05 '22 at 00:38
0

Arrays are not declared globally, they are declared where you declare them :-)

In your case, you declare it at the top of main() so that is its scope, from point of declaration to end of main(). Trying to use it in lastSearch() is therefore invalid.

The easiest fix is probably just to move the declaration immediately before main() so that it is global. But the easiest things is often not the right thing.

You would be better off embracing C++ fully(1) and using something like std::vector, whose size isn't arbitrarily limited to 100 (for example) and which you could pass around quite easily, something like:

#include <iostream>
#include <vector>

void function(const std::vector<int> &vec) {
    std::cout << vec.size() << ' ' << vec[0] << '\n'; // Output: 2 42
}

int main() {
    std::vector<int> x;
    x.push_back(42);
    x.push_back(99);
    function(x);
}

The main advantages with vectors are that:

  • you're not limited to a maximum of 100 items;
  • you don't have to pass around the actual count of items read separately as with a raw array or even a std::array (you don't do that in your code but I assure you, that's a problem).
  • the size of the vector is an integral property of the vector, available anywhere the vector is in scope.

(1) There's a variety of developers I like to call C+ developers. These are the people that, though they claim to be C++ developers, have never really embraced the C++ way of doing things, sticking to C style programming practices like non-smart pointers or normal arrays :-)

Some of those things may still have a place in modern C++ code but you should be circumspect in their use.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953