Andrew Hudson
Andrew Hudson

Reputation: 13

C++ array size by user input

I'm trying to write a program that will have a user input size for an array, and then take values into that array. I initially tried

int sz = 51;
double Arr[sz];

Which led to compilation errors. Apparently dynamic allocation of the variable has to happen, and I'd rather avoid that if possible. So I modified my code (current state as shown below) which now only throws "expected primary-expression before ']' token". Is there a way to fix this and I'm just not seeing it, or do I need to use dynamic allocation?

Thanks for your time!

#include <iostream>
#include <iomanip> //for setprecision

using namespace std;

int sz = 51;
double n=0;
double Arr[0];

void get_input(double Arr[], int &sz){  //gets input
do{
    cout<< "Enter size: "<< endl;
    cin>> sz;
    if (sz<0 || sz>50){
        cout<< "Invalid size, enter a value between 0 and 50"<<endl;
    }
}while(sz<0 || sz>50);

for( int i=0; i<sz; i++){
    cin>> Arr[i];
}
}

double calcSum( double Arr[], int sz){ //finds sum
for(int i=0; i<sz; i++){
    n+= Arr[i];
}
return(n);
}

void printArray(double Arr[], int sz){ //prints array elements
for(int i=0; i<sz; i++){
    cout<< Arr[i]<< setprecision(2)<<" ";
    if(i%7 == 0)
        cout<< endl;
}
}


int main()
{
double Arr[sz];
get_input(Arr[], sz); //error here
printArray(Arr[], sz); //error here

    return 0;
}

Upvotes: 1

Views: 12503

Answers (2)

sehe
sehe

Reputation: 392833

Just use a std::vector, there's a standard library in C++ for this reason.

Demo:

  • notes: you don't need the globals (they are shadowed by the locals and you pass them by reference anyways)

Live On Coliru

#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;
using array_t = std::vector<double>;

void get_input(array_t& Arr) { // gets input
    size_t sz = 51; // unsigned types cannot be negative
    do {
        cout << "Enter size: " << endl;
        cin >> sz;
        if (sz > 50) {
            cout << "Invalid size, enter a value between 0 and 50" << endl;
        }
    } while (sz > 50);

    for (size_t i = 0; i < sz; ++i) {
        double v;
        if (cin >> v)
            Arr.push_back(v);
        else
            std::cerr << "Error reading input\n";
    }

    //assert(sz = Arr.size());
}

double calcSum(array_t const& Arr) { // finds sum
    double n = 0;
    for (size_t i = 0; i < Arr.size(); ++i) {
        n += Arr[i];
    }
    return n;
}

void printArray(array_t const& Arr) { // prints array elements
    for (size_t i = 0; i < Arr.size(); ++i) {
        cout << Arr[i] << setprecision(2) << " ";
        if (i % 7 == 6)
            cout << endl;
    }
}

int main() {
    array_t Arr;
    get_input(Arr);
    printArray(Arr);
    std::cout << "\nSum: " << calcSum(Arr) << "\n";
}

When entering 3 1 2 3 you get:

Enter size: 3
1 2 3
1 2 3
Sum: 6

Upvotes: 4

Jason
Jason

Reputation: 3917

VLAs (e.g. Arr[sz]) are only supported as extensions in C++. They aren't part of the official language standard. You should use std::vector instead.

Upvotes: 5

Related Questions