c++vectorsubscript

subscripted value is not an array, pointer, or vector on index variable


I am receiving the error titled above, "subscripted value is not an array, pointer, or vector", on all of my [i] instances, and can not seem to understand why. Another program I have done with vectors recently does not seem to have any noticeable difference and works fine. Does anyone see what I am missing?

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int getData() {
    vector<int> weeklyScore(16, 0);

    for (int i = 0; i < 16; i++) {
        cout << "Enter quiz score (0-15) for week " << i+1 << " : " << endl;
        cin >> weeklyScore[i];

        if (weeklyScore[i] < 0 || weeklyScore[i] > 15) {
            cout << "Please enter a valid score (0-15): " << endl;
            i--;
        }
    }

    for (int i = 0; i < 16; i++) {
        cout << "Module "<< i+1 << ": " << weeklyScore[i] << endl;
    }

    return 0;
}

int highScore() {
    auto weeklyScore = getData();
    int high = 0;
    int i;
    int high;

    for (i = 0; i < 16; i++)
        if (weeklyScore[i] > high) {
            high = weeklyScore[i];
            ihigh = i;
        }

    cout << "The highest score is " << high << " in module "<< ihigh+1 << endl;
    return 0;
}

int lowScore() {
    auto weeklyScore = getData();
    int low = 16;
    int i;
    int ilow;

    for (i = 0; i < 16; i++)
        if (weeklyScore[i] < low) {
            low = weeklyScore[i];
            ilow = i;
        }

    cout << "The lowest score is " << low << " in module "<< ilow+1 << endl;
    return 0;
}

int ave() {
    auto weeklyScore = getData();
    int i;
    int average;
    int sum = 0;

    for (i = 0; i < 16; i++)
        sum = sum + weeklyScore[i];

    average = sum / 16;
    return 0;
}

int main() {
    highScore();
    lowScore();
    ave();
    return 0;
}

Solution

  • int getData(){
        ...
        return 0;
    }
    
    auto weeklyScore = getData();
    

    For some reason you made getData return an int therefore the compiler determines that the auto in auto weeklyScore = getData(); is also an int, not the vector you want it to be.

    Since it seems that you want getData to return a vector of weekly scores you should change the function to do that.

    vector<int> getData(){
        ...
        return weeklyScore;
    }
    

    This seems like a variation on the common misunderstanding that variables with the same name but in different functions have some connection to each other. This is not true. The variable weeklyScore in getData is a different variable to weeklyScore in highScore. There's no reason that these two different variables must have the same type or the same values. It is up to you to make that so (if that is what you want).

    Another point, you call the function getData three times so this program is going to ask the user to input three sets of data. I doubt that is what you want. Instead you should call the function getData once in main. Save the scores in a variable in main and then pass that variable as a parameter to each of your three functions highScore, lowScore, and ave.

    It also seems likely (to me) that the functions highScore, lowScore, and ave are supposed to return the values that they calculate (instead of just returning zero) and the returned values are supposed to be printed in main. But this is just my guess.

    Understanding how function parameters and function return values work is an important topic when you are learning C++. Seems likely that this exercise is designed to teach you how that works.