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;
}
For some reason you made
getDatareturn aninttherefore the compiler determines that theautoinauto weeklyScore = getData();is also anint, not the vector you want it to be.Since it seems that you want
getDatato return a vector of weekly scores you should change the function to do that.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
weeklyScoreingetDatais a different variable toweeklyScoreinhighScore. 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
getDatathree 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 functiongetDataonce inmain. Save the scores in a variable inmainand then pass that variable as a parameter to each of your three functionshighScore,lowScore, andave.It also seems likely (to me) that the functions
highScore,lowScore, andaveare supposed to return the values that they calculate (instead of just returning zero) and the returned values are supposed to be printed inmain. 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.