1

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;
}
3
  • Your error is that get_input(Arr[]) makes no sense. Arr is already an array, you don't need to keep putting [] there, unless you want to use the subscript operator. Also, sz is still not a constant expression. Make it const int sz = 51; and that will work. Minus the parts where you try to modify sz. Commented Nov 9, 2015 at 23:21
  • what compiler is this for? Commented Nov 9, 2015 at 23:22
  • It's the GNU GCC compiler Commented Nov 10, 2015 at 0:06

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

OP most certainly doesn't know (and doesn't need to know) what a VLA is, and probably doesn't know what you mean by "extension". You better explain that, or even better, remove it.
I agree, except I think it's a good answer and should be deleted. If we don't tell, the OP will never know :) I tend to assume curious and learn-eager OPs
I don't care about the terminology, but it isn't part of the language standard and you shouldn't use it.
4

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

2 Comments

Yeah, I wan't quite prepared for the amount of confusion there with the globals, and I don't like to post it when I'm still testing. @πάνταῥεῖ
I'm acting similar most of the time recently, also because being a bit restricted with my tablet (that's also the reason why I refused to chat in the recent time, it's not funny or satisfying much from that device).

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.