1

See line 91 of code,

array<double,size1> poundData; //PROBLEM HERE array declared

Global constant size1 can be used to represent integer value 5 everywhere else in code but returns an error if used for declaring the array length.

If size1 is replaced with 5 on this line, code runs perfectly, but need size1 to be used for flexibility. Cannot see why it won't allow size1 to be used, but am very much a beginner so may be something very basic i'm missing.

#include<iostream>
#include <cstdlib>
#include<time.h>
#include <array>

using namespace std;

const int size1 = 5; // declare global constant

double randValInRange (double min,double max,int size1,int i); //Declare functions

array<double,size1> fillArray(double min,double max,int size1);


int main (int argc, char *argv[])
{
  
  
  if (argc >= 3)   //Determine if there are at least 3 command line arguments
    {
      double min;
      double max;
      double input1;
      double input2;
      input1 = atof (argv[1]);
      input2 = atof (argv[2]);

      if (input1 <= 0.0||input2 <= 0.0) {     // Determine that both inputs are positive
      cout << " \n min and max must both be positive values\n";

      return EXIT_SUCCESS;
        }
       
      if (input1 < input2){
      min = input1;
      max = input2;
    }else{

      min = input2;
      max = input1;
    }

      cout << min << " lbs is min \n";
      cout << max << " lbs is max \n\n";

      srand(static_cast<unsigned int>(time(nullptr)));

      // std::
    array<double,size1> poundData;

      poundData =  fillArray(min,max,size1);
   

      
   
      for (int i=0;i<size1;i++) {
    cout<<poundData[i]<<"   ";

    cout<<"\n";
        }

} else {

  cout << "Incorrect number of arguments - please call as follows: " <<argv[0] << " min max\n";
    }
  return 0;
}

double randValInRange (double min,double max,int size1,int i ){
    
  double scale = 100000.0;
  double range = max - min;
    
  double rangeScaled = range*scale;
  int rangeScaledInt = (static_cast<int>(rangeScaled));
    
  double minScaled = min * scale;
  int minScaledInt = (static_cast<int>(minScaled));
    
  int randIntScaled = rand()%(rangeScaledInt);
  int outputValueScaled = minScaledInt + randIntScaled;
  double outputValueScaledD = static_cast<double>(outputValueScaled);
  double outputValue = outputValueScaledD/scale;

  return outputValue;
}

array<double,size1> fillArray (double min,double max,int size1) //function with return type array
{
 
  array<double,size1> poundData; //**PROBLEM HERE** array declared
    
  
  for (int i=0;i<size1;i++) {

    double outputValue = randValInRange (min,max,size1,i);
    poundData[i] = outputValue;
     
        }
  return poundData;

}
finnh98
  • 11
  • 2
  • You can use `::` scope operator like `array` to avoid this problem. This is explained in any [beginner c++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) which are also available as PDFs for free. – Jason Oct 21 '22 at 12:31

2 Answers2

2

Here

const int size1 = 5; // declare global constant

size1 is a constant expression. For clarity you could have used constexpr:

constexpr int size1 = 5;

Here:

double randValInRange (double min,double max,int size1,int i ){

The parameter called size1 shadows the global variable of same name. Inside the function size1 refers to the argument which is not a constant expression.

Use more meaningful names than size1, then such name clashes are less likely.

And better avoid global variables. For example rather than defining the size globally you could have declared a type alias globally:

using array_type = std::array<double,5>;

Well, actually this isnt really much better than the int when declared globally, but for now it would avoid your current issue.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
1
array<double,size1> fillArray (double min,double max,int size1/*!!!*/)
{                           
// here size1 is a parameter and this hide the global constant size1
}
jls28
  • 79
  • 1
  • 1
  • 6