Hi, first of all I would like to thank you for taking the time to look at my problem.

I'm trying to input to a multidimensional character array. The error occurs on the second array. I tried to understand the problem by varying my input, but it always occurs on the second array. It prompts for a second entry though. If you set the size as one and enter an applicable character array it returns to main and outputs that array.

I've tried to Google an answer but I couldn't identify my error. They always showed integer arrays. I think I could probably add the string library and make this all simple, but I'm a bit curious of how to do this with an actual character array and I have never used the string functions before.

Thank You for your time.

#include<iostream>
using namespace std;

int getsize();
void subdir(char **sub, int size);



int main()
{
int size,x; char *sub[10];
size = getsize();
*sub = new char[size];
subdir(sub,size);
for(x=0;x<size;x++)
	cout << endl << sub[x] << endl;
}


		int getsize()
		{
		int size;
		cout << "Enter the size of array ";
		cin >> size;
		return size;
		}



void subdir(char **sub,int size)
{
int x,y; char temp[12];
cout << "Enter Names in order\n";
for(x=0;x<size;x++)
	{
	cin >> temp;
	for(y=0;temp[y]!=0;y++)
		sub[x][y] = temp[y];
	sub[x][y] = 0; 
	}

}

Yes, character arrays can be a bit tricky, and passing and returning arrays can be tricky. There is more than one way to do what you are doing. This way does not use dynamic memory. I'm not sure if you are trying to use dynamic memory or not. Here's a way of doing it using a strict 2-D non-dynamic array.

#include <iostream>
using namespace std;

void FillArray (char nameArray[][30], int numNames)
{
     for (int i = 0; i < numNames; i++)
     {
         cout << "Enter name " << i << " : ";
         cin >> nameArray[i];
     }
}


int main ()
{
    char nameArray[10][30];
    
    FillArray (nameArray, 10);
    for (int i = 0; i < 10; i++)
        cout << nameArray[i] << endl;
        
    cin.get ();
    cin.get ();
    return 0;
}

Line 16 reserves 300 characters of space. It is assumed that the 300 characters will store 10 individual strings, each with a maximum length of 30 (including null terminator). Try entering a name that has 30 or more characters and see what happens.

The 300 characters above are guaranteed to be contiguous. Do it this way (dynamically) and that may or may not be the case.

char** nameArray;
nameArray = new char*[10];
for (int i = 0; i < 10; i++)
    nameArray[i] = new char[30];

Note that, unlike in your code, I have the "new" command execute 10 times when allocating space for my characters. What are you entering inside of your getsize() function? If you want 10 strings of 30 characters each, you would need to enter ten times thirty, or 300, to reserve enough space using the code that you have. Remember, there are two sizes, not one. One size is the number of strings. The other is the number of characters in each string. It's unclear to me what you intend in the getsize () function.

Thank You for your response. I get the problem now.

The rows are supposed to be derived from the int getsize() function. Thank You so much for your help in declaring char *sub[10]; I was under the misconception that I was initializing the entire array. Thinking back I do remember seeing something to this extent, but your explanation helped me understand the problem vs. hacking the code.

Thanks Once Again for all of your Help


Correct Code

#include<iostream>
using namespace std;

int getsize();
void subdir(char **sub, int size);



int main()
{
int size,x; char **sub;
size = getsize();
sub = new char*[size];
for(x=0;x<size;x++)
	sub[x] = new char[12];
subdir(sub,size);
for(x=0;x<size;x++)
	cout << endl << sub[x] << endl;
}


		int getsize()
		{
		int size;
		cout << "Enter the number of subdirectories ";
		cin >> size;
		return size;
		}



void subdir(char **sub,int size)
{
int x;
cout << "Enter Names in order\n";
for(x=0;x<size;x++)
	cin >> sub[x];
return;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.