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);
}
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];
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.