0

I'm getting a variable array problem in Sublime Text 3 even after building the system with MinGW64.

#include <bits/stdc++.h>                                                    
using namespace std;                                                        

int main()                                                                  
{                                                                           
    int a,b;                                                                
    cin>>a>>b;                                                              
    cout<<(a+b)<<endl;                                                      
    int n;                                                                  
    int arr[n];                                                             
    for(int i=0;i<n;i++)                                                    
    {                                                                       
        cin >> arr[i];                                                      
    }                                                                       
    for(int i=0;i<n;i++)                                                    
    {                                                                       

        cout<<arr[i]<<endl;                                                 
    }                                                                       
    cout<<"hello";                                                          
    return(0);                                                              
}

Sample Error

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • 3
    Welcome to Stack Overflow! Please consider adding more detail and adding your error code and source code for better readability. Thank you. – creyD Mar 15 '19 at 04:13
  • I've removed the sublime tag because your problem is related to your code and not to Sublime Text. I will point out however that it looks like your code (when you get it working) may be trying to read input interactively from the user. That won't work in Sublime because you can't interact with running programs. – OdatNurd Mar 15 '19 at 04:41
  • 1
    you have not provided the value of 'n', it is some garbage value, fix it and try again. – Mahendra suthar Mar 15 '19 at 04:46

2 Answers2

1

You have not provided the value of 'n', it is some garbage value, may be you want to define a macro using

#define N 10 //say

then you can use this to make a array of size N

int arr[N];  
Mahendra suthar
  • 401
  • 5
  • 18
0

ISO C++ forbids variable length array 'arr' tells the whole story. In C++, the size of arrays need to be known at compile time since they're statically allocated, whereas right now you're trying to set the size of the array at run time since you're obtaining the size of the array from n, which is a variable. There's the other issue that Mahendra suthar points out that n is uninitialized, but I'm assuming that you want the size of the array to be changeable at run time based on a and b, so you'll either need to use dynamic memory allocation or use std::vector:

#include <bits/stdc++.h>
#include <vector>
using namespace std;                                                           

int main()                                                                     
{                                                                              
    int a,b;                                                                   
    cin>>a>>b;                                                                 
    cout<<(a+b)<<endl;                                                         
    int n;                                                                     
    std::vector<int> arr;                                                                
    for(int i=0;i<n;i++)                                                       
    {    
        int temp;

        cin >> temp;
        arr.push_back(temp);                                                         
    }                                                                          
    for(int i=0;i<n;i++)                                                       
    {                                                                          

        cout<<arr[i]<<endl;                                                    
    }                                                                          
    cout<<"hello";                                                             
    return(0);                                                                 
}
Chi
  • 322
  • 2
  • 10
  • @rayryng I agree that compilers are perfectly capable of doing exactly what you describe, and some implement this functionality, but the issue here is that the ISO C++ standards don't support this behavior and compiler output is describing that the run time declaration of n is the problem. – Chi Mar 15 '19 at 05:39