|
| |||||||||
| Tags: arrays, cpp |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| ||||
| ||||
| Problem with arrays in C++
Please help me with the code as below. It gives me an error. I am unable to rectify it. Code: #include <iostream.h>
void main ()
{
int size, i;
cout<<"Enter the array length:";
cin>>size;
cout<<endl;
char* arr[size];
cout<<"Enter the array items:"<<endl;
for (i=1;i<=size;i++)
{
cout<<i<<".";
cin>>arr[i];
}
cout<<endl<<"The array items:"<<endl;
for(i=1;i<=size;i++)
{
cout<<i<<".";
cout<<arr[i]<<endl;
}
} |
|
#2
| ||||
| ||||
|
What you are doing is you are defining the array at runtime but not during the compile time which is not allowed in C/C++. According to your code you can define the array at the runtime by using the new and delete operators. Here is the code how you do that Code:
int size, i;
char *arr;
cout<<"Enter the array length:";
cin>>size;
cout<<endl;
arr = new char[size]; Hope this helps you. |
|
#3
| ||||
| ||||
|
Could you tell us what the error was? |
|
#4
| ||||
| ||||
|
Thanks unlimitedtech for the help. It worked. |
![]() |
|
| Thread Tools | Search this Thread |
| |
Similar Threads for: "Problem with arrays in C++" | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Arrays in C# | kavisg1 | Software Development | 3 | 07-10-2010 08:55 PM |
| Problem with 4+ dimension allocatable arrays and optimized code | Tamonash | Software Development | 6 | 30-09-2010 12:59 AM |
| How to use an Arrays in PHP? | Jacques25 | Software Development | 4 | 25-02-2010 01:24 AM |
| Problem printing like integers in C# ARRAYS | Lambard | Software Development | 4 | 26-01-2009 04:55 PM |
| Arrays in C# | DotNetUser | Guides & Tutorials | 2 | 03-12-2008 05:31 PM |