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: shaynerossum is an unknown quantity at this point 
Solved Threads: 0
shaynerossum shaynerossum is offline Offline
Newbie Poster

cin of multi-dimension character array

 
0
  #1
Sep 25th, 2009
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.

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int getsize();
  5. void subdir(char **sub, int size);
  6.  
  7.  
  8.  
  9. int main()
  10. {
  11. int size,x; char *sub[10];
  12. size = getsize();
  13. *sub = new char[size];
  14. subdir(sub,size);
  15. for(x=0;x<size;x++)
  16. cout << endl << sub[x] << endl;
  17. }
  18.  
  19.  
  20. int getsize()
  21. {
  22. int size;
  23. cout << "Enter the size of array ";
  24. cin >> size;
  25. return size;
  26. }
  27.  
  28.  
  29.  
  30. void subdir(char **sub,int size)
  31. {
  32. int x,y; char temp[12];
  33. cout << "Enter Names in order\n";
  34. for(x=0;x<size;x++)
  35. {
  36. cin >> temp;
  37. for(y=0;temp[y]!=0;y++)
  38. sub[x][y] = temp[y];
  39. sub[x][y] = 0;
  40. }
  41.  
  42. }
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,844
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: cin of multi-dimension character array

 
0
  #2
Sep 25th, 2009
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.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void FillArray (char nameArray[][30], int numNames)
  5. {
  6. for (int i = 0; i < numNames; i++)
  7. {
  8. cout << "Enter name " << i << " : ";
  9. cin >> nameArray[i];
  10. }
  11. }
  12.  
  13.  
  14. int main ()
  15. {
  16. char nameArray[10][30];
  17.  
  18. FillArray (nameArray, 10);
  19. for (int i = 0; i < 10; i++)
  20. cout << nameArray[i] << endl;
  21.  
  22. cin.get ();
  23. cin.get ();
  24. return 0;
  25. }

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.

  1. char** nameArray;
  2. nameArray = new char*[10];
  3. for (int i = 0; i < 10; i++)
  4. 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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 10
Reputation: shaynerossum is an unknown quantity at this point 
Solved Threads: 0
shaynerossum shaynerossum is offline Offline
Newbie Poster

Re: cin of multi-dimension character array

 
0
  #3
Sep 26th, 2009
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

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int getsize();
  5. void subdir(char **sub, int size);
  6.  
  7.  
  8.  
  9. int main()
  10. {
  11. int size,x; char **sub;
  12. size = getsize();
  13. sub = new char*[size];
  14. for(x=0;x<size;x++)
  15. sub[x] = new char[12];
  16. subdir(sub,size);
  17. for(x=0;x<size;x++)
  18. cout << endl << sub[x] << endl;
  19. }
  20.  
  21.  
  22. int getsize()
  23. {
  24. int size;
  25. cout << "Enter the number of subdirectories ";
  26. cin >> size;
  27. return size;
  28. }
  29.  
  30.  
  31.  
  32. void subdir(char **sub,int size)
  33. {
  34. int x;
  35. cout << "Enter Names in order\n";
  36. for(x=0;x<size;x++)
  37. cin >> sub[x];
  38. return;
  39. }
Last edited by shaynerossum; Sep 26th, 2009 at 12:07 am. Reason: Added corrected code for future thread viewers
Reply With Quote Quick reply to this message  
Reply


This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 771 | Replies: 2
Thread Tools Search this Thread



Tag cloud for characterarray, dynamiccharacterarray, multipledimensionarray
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC