Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 05-09-2008
Member
 
Join Date: May 2008
Posts: 685
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;
	}
}
Reply With Quote
  #2  
Old 05-09-2008
Member
 
Join Date: May 2008
Posts: 186
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];
And remember you free the memory at the end of the code using delete operator.

Hope this helps you.
Reply With Quote
  #3  
Old 05-09-2008
Member
 
Join Date: May 2008
Posts: 1,395
Could you tell us what the error was?
Reply With Quote
  #4  
Old 05-09-2008
Member
 
Join Date: May 2008
Posts: 685
Thanks unlimitedtech for the help. It worked.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem with arrays in C++"
Thread Thread Starter Forum Replies Last Post
Arrays in C++ Acolapissa Software Development 3 19-12-2010 10:05 AM
Arrays in C# kavisg1 Software Development 3 07-10-2010 07:55 PM
Problem with 4+ dimension allocatable arrays and optimized code Tamonash Software Development 6 29-09-2010 11:59 PM
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


All times are GMT +5.5. The time now is 05:15 AM.