Home > Net >  Why is my variable not declared in the scope?
Why is my variable not declared in the scope?

Time:03-05

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

CodePudding user response:

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.

CodePudding user response:

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

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

  • Related