0

I am receiving the following error: Subscripted value is not an array, pointer, or vector. Can anyone advise on how to fix this issue? I am receiving the error in the void GetData function at the bottom on the line that reads " vals[valCount] = value; ". Thank you!

#include <iostream>
#include <iomanip>
#include <array>

using namespace std;

//function declarations
void GetData(double vals, int valCount);
void Sort(double vals, int valCount);
double Variance(double vals, int valCount);
double StandardDev(double vals, int valCount);
double SqRoot(double value); //use for StandardDev function

//function definitions
int main ()
{
    double sum = 0,
        variance = 0,
        standardDev = 0,
        vals = 0;

    int valCount = 0;        //number of values to be processed

    //ask user how many values
    cout << "Enter the number of values (0 - 100) to be processed: ";
    cin >> valCount;

    //process and store input values
    GetData(vals, valCount);

    //output
    cout << "\nValues in Sorted Order: " << sum;
    cout << "\n\nThe variance for the input value list is: " << variance;
    cout << "\nThe standard deviation for the input list is: " << standardDev;
    cout << "\n\nPress any value to exit this program\n" << endl;

    return 0;

}

//process and store data
void GetData(double vals, int valCount)
{
    int value; valCount = 0;

    cout << "Enter a value: ";
    cin >> value;

    while (value != 0) {
        vals[valCount] = value;
        valCount++;

    cout << "/nEnter a value: ";
    cin >> value;
    }
}
1
  • 2
    vals is not an array/vector: double vals. Perhaps it should be double *vals. Commented Apr 14, 2020 at 14:55

2 Answers 2

1

vals is a double which means it can only hold one value. You need an array or vector. Also in GetData you loop while value isn't 0 but you already got valCount from the user, so you should probable use a for loop to loop this number of times.

#include <iostream>
#include <iomanip>
#include <array>

using namespace std;

//function declarations
void GetData(double* vals, int valCount);
void Sort(double* vals, int valCount);
double Variance(double* vals, int valCount);
double StandardDev(double* vals, int valCount);
double SqRoot(double value); //use for StandardDev function

//function definitions
int main ()
{
    double sum = 0,
        variance = 0,
        standardDev = 0;
    double vals[100];

    int valCount = 0;        //number of values to be processed

    //ask user how many values
    cout << "Enter the number of values (0 - 100) to be processed: ";
    cin >> valCount;

    //process and store input values
    GetData(vals, valCount);

    //output
    cout << "\nValues in Sorted Order: " << sum;
    cout << "\n\nThe variance for the input value list is: " << variance;
    cout << "\nThe standard deviation for the input list is: " << standardDev;
    cout << "\n\nPress any value to exit this program\n" << endl;

    return 0;
}

//process and store data
void GetData(double* vals, int valCount)
{
    for(int i = 0; i < valCount; i++)
    {
        cout << "Enter a value: ";
        cin >> vals[i];
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you! I tried this, but now I am receiving an error here: //process and store input values GetData(vals, valCount) "No matching function for call to 'GetData'"
I added an "&" in the main function when referencing the GetData function and it worked! Thanks for your help!
@larry718 you shouldn't need an "&". Did you declare vals as double vals; or double vals[100];?
0

vals in your code is not an array.
Hence, you cannot do vals[x], which means accessing the element at xth index.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.