Results 1 to 4 of 4

Thread: Problem with arrays in C++

  1. #1
    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;
    	}
    }

  2. #2
    Join Date
    May 2008
    Posts
    188
    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.

  3. #3
    Join Date
    May 2008
    Posts
    1,395
    Could you tell us what the error was?

  4. #4
    Join Date
    May 2008
    Posts
    685
    Thanks unlimitedtech for the help. It worked.

Similar Threads

  1. Arrays in C++
    By Acolapissa in forum Software Development
    Replies: 3
    Last Post: 19-12-2010, 10:05 AM
  2. Arrays in C#
    By kavisg1 in forum Software Development
    Replies: 3
    Last Post: 07-10-2010, 07:55 PM
  3. Problem with 4+ dimension allocatable arrays and optimized code
    By Tamonash in forum Software Development
    Replies: 6
    Last Post: 29-09-2010, 11:59 PM
  4. Problem printing like integers in C# ARRAYS
    By Lambard in forum Software Development
    Replies: 4
    Last Post: 26-01-2009, 04:55 PM
  5. Arrays in C#
    By DotNetUser in forum Guides & Tutorials
    Replies: 2
    Last Post: 03-12-2008, 05:31 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,289,611.46218 seconds with 17 queries