#include <iostream>
using namespace std;
int main () {
int x;
 
cin>>x>>"\t";
 int A = A[x];

return 0;
}

the error is "subscript requires array or pointer type"


how can i input my desired number of size by using cin for x??? help

1.I think you are trying to create an int array with a size which input by the user.

First, the syntax for creating an integer array is

int array_name[size];

Secondly,
cin is used to input data into a variable and hence cin>>x>>"\t" is wrong
The >>"\t" part is incorrect as it is not a variable.

To accept a value for integer x through user input, the syntax is

cin>>x;

So the proper code would be

#include <iostream>
using namespace std;
int main () {
int x;

cin>>x;
int A[x];

return 0;
}

2.If you are looking to input values into the array, use the code:

cin>>A[0];

This code will accept a value for the first element (which has index no. 0) of array A.
For the next element, use A[1], A[2] and so on...

Edited 6 Years Ago by anantk: n/a

to anantk

just a newb myself but i was under the impression that the size of an array had to be specified by a constant not a variable entered by the user. i may have this wrong.

to anantk

just a newb myself but i was under the impression that the size of an array had to be specified by a constant not a variable entered by the user. i may have this wrong.

AFAIK, what you say holds true for the older compilers like Borland/Turbo C++..

Most new compilers (like CodeBlocks, which I use) support variable sized arrays as in my previous post.
Older ones like Turbo C++ need to use dynamic memory allocation (using 'new', pointers etc.)
Someone asked about that here too : http://www.cplusplus.com/forum/beginner/1601/

This article has been dead for over six months. Start a new discussion instead.