943,656 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1482
  • C++ RSS
Sep 25th, 2009
0

cin of multi-dimension character array

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 1
Newbie Poster
shaynerossum is offline Offline
13 posts
since Apr 2009
Sep 25th, 2009
0

Re: cin of multi-dimension character array

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.

C++ Syntax (Toggle Plain Text)
  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.

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,372 posts
since Jan 2008
Sep 26th, 2009
0

Re: cin of multi-dimension character array

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

C++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Newbie Poster
shaynerossum is offline Offline
13 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Binary Search Using Recursion on 2-D Array
Next Thread in C++ Forum Timeline: Book Suggestion





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC