| | |
cin of multi-dimension character array
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2009
Posts: 10
Reputation:
Solved Threads: 0
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.
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.
C++ Syntax (Toggle Plain Text)
#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; } }
•
•
Join Date: Jan 2008
Posts: 3,844
Reputation:
Solved Threads: 503
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.
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.
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.
C++ Syntax (Toggle Plain Text)
#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.
C++ Syntax (Toggle Plain Text)
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.
Last edited by VernonDozier; Sep 25th, 2009 at 10:37 pm.
•
•
Join Date: Apr 2009
Posts: 10
Reputation:
Solved Threads: 0
Thank You for your response. I get the problem now.
The rows are supposed to be derived from the
Thank You so much for your help in declaring
Thanks Once Again for all of your Help
Correct Code
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
C++ Syntax (Toggle Plain Text)
#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; }
Last edited by shaynerossum; Sep 26th, 2009 at 12:07 am. Reason: Added corrected code for future thread viewers
![]() |
Similar Threads
- Character array (C++)
- multi dimernsion array with classes (C++)
- Converting from a string to a character array. (C++)
- How do you define character array constant (C++)
- multi dimension array (Ruby)
- Character Array to character pointer. (C)
- Help with folding a character array please (C)
Other Threads in the C++ Forum
- Previous Thread: Binary Search Using Recursion on 2-D Array
- Next Thread: Book Suggestion
Views: 771 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for characterarray, dynamiccharacterarray, multipledimensionarray







