I cannot figure out what this error message means?

#include <iostream>
using namespace std;


double max_array(const double x[], int max_val); 
int main (void)
{    
    int i, max_val; 
    double x[max_val];
    double array_avg, array_max, array_min;
    cout<<"Enter likely number of values: ";
    cin>>max_val;
    cout<<"Enter values seperated by white space:";
     for (i=0;i<max_val;i++)
        cin>>x[i];
    cout<<"\nThese values were entered: ";
    for(i=0;i<max_val;i++){
         cout<<x[i]<<" ";
    array_max=max_array(x[i], max_val);  
    }
   
    
system("pause");
return 0;
}
double max_array(const double x[], int max_val)
{
     double max_int;
     
     max_int=x[0];
     for (int k=1; k<max_val; k++)
     {
         if (x[k]>max_val)
             max_int=x[k];
     }
return max_int;
}

could someone explain to me what i did wrong?

max_val is supposed to tell the program how many numbers will be inputed. Then the size array x is the value of max_val...if that makes sense.

lines 8 and 9

int i, max_val; 
    double x[max_val];

What is the value of max_val? What is the size of array x?

max_val is supposed to tell the program how many numbers will be inputed. Then the size array x is the value of max_val...if that makes sense.

Like what Ancient Dragon is hinting at...what is max_val equal to when line 9 is to use the max_val?

An array must have a value for its size when being initialized, and max_val, at line 9, is left without a value

Edited 6 Years Ago by josolanes: n/a

Comments
exactly :)

So I should have defined max_val beforehand? I'm sorry I'm fairly new with c++. The main function ran fine before I added the max_array function..

Like what Ancient Dragon is hinting at...what is max_val equal to when line 9 is to use the max_val?

An array must have a value for its size when being initialized, and max_val, at line 9, is left without a value

So I should have defined max_val beforehand? I'm sorry I'm fairly new with c++. The main function ran fine before I added the max_array function..

Yep, just move the declaration for array x after asking the user for the value of max_val, and that should correct this issue. This way, max_val will have a value before trying to create your array

This article has been dead for over six months. Start a new discussion instead.