Passing a 2D array of characters to a function

Started by
6 comments, last by Zahlman 14 years, 10 months ago
Ok, I am having a serious brain lapse in how to do the following: I want the function printDatabase(int a[][NUM_GRADES], char names[][20]) to simply print out the information contained inside of a and names. However names is defined as the following

char studentNames[NUM_STUDENTS][20] = {{"Bob"}, {"John"}, {"Joe"}};

The function definition of printDatabase is the following:

// output data
void printDatabase( int a[][ NUM_GRADES ], const char names[][20] )
{
   cout << "Here is the grade database\n\n"
        << setw( 10 ) << "Name";
   
   for ( int n = 1; n <= NUM_GRADES; n++ )
      cout << setw( 4 ) << n;

   cout << endl;
   
   for ( int i = 0; i < NUM_SUDENTS; i++ ) {
      cout << setw( 10 ) << names;
      
      <span class="cpp-keyword">for</span> ( <span class="cpp-keyword">int</span> j = <span class="cpp-number">0</span>; j &lt; NUM_GRADES; j++ )
         cout &lt;&lt; setw( <span class="cpp-number">4</span> ) &lt;&lt; a[ i, j ];

      cout &lt;&lt; endl;

   } <span class="cpp-comment">// end for</span>

   cout &lt;&lt; endl;

} <span class="cpp-comment">// end printDatabase</span>

</pre></div><!–ENDSCRIPT–>

The problem I am having is that I am throwing an error every time I try and do this and I can not remember how your suppose to pass the char array into the function. This is being done as exercises out of a book for my own personal enjoyment :). Thanks for any help.
Advertisement
void foo( char** ppStrings );

or

void foo( char* pStrings[] );
While it will teach you a tad about memory and pointers, this is retarded. Sounds alot like a school assignment (the retardedness was a dead give away).

try this:
std::vector<std::string> studentNames;studentNames.resize(3);studentNames[0] = "Bob";studentNames[1] = "John";studentNames[2] = "Joe";void Foo(std::vector<std::string> &students){ size_t sCnt = students.size(); for (size_t s = 0; s < sCnt; ++s) {  //print some shiz }}


or even:
struct Student{ Student(const std::string &name = "")  : Name(name) { } std::string Name; std::vector<int> TestScores;};std::vector<Student> students;students.resize(3);students[0] = Student("Bob");students[1] = Student("John");students[2] = Student("Joe");void Foo(std::vector<std::string> &students){ size_t sCnt = students.size(); for (size_t s = 0; s < sCnt; ++s) {  Student &stud = students;<br>  cout &lt;&lt; "Name: " &lt;&lt; stud.Name &lt;&lt; endl;<br>  cout &lt;&lt; "  Scores: ";<br><br>  size_t tCnt = stud.TestScores.size();<br>  for (size_t t = 0; t &lt; tCnt; ++t)<br>  {<br>   cout &lt;&lt; stud.TestScores[t] &lt;&lt; " ";<br>  }<br><br>  cout &lt;&lt; endl;<br> }<br>}<br></pre>
Quote:Original post by toogreat4u
char studentNames[NUM_STUDENTS][20] = {{"Bob"}, {"John"}, {"Joe"}};

The initializer on the right does not fit the data type on the left.
{{"Bob"}, {"John"}, {"Joe"}} is a three dimensional array.
Maybe {"Bob", "John", "Joe"} works better?

But as already suggested, you should use the tools C++ priovides, namely strings and vectors.
After re-looking over the code I corrected some things and sure enough the function that I thought was causing issues was not where I had the error. The char double subscripted array is perfectly valid way of passing the information to a function. I do appreciate the responses and I am aware of all the STL headers and how they work (mostly), however I am trying to revamp my knowledge of everything. Thanks!
First off,

Quote:After re-looking over the code I corrected some things and sure enough the function that I thought was causing issues was not where I had the error.


You are aware, I hope, that error messages generally include a line number and a description of the problem? If they don't make sense, just fix the first one; sometimes a missing semicolon or whatever can cause a lot of confusion for the compiler, which doesn't read code in a very "human" way at all.

Quote:Original post by toogreat4u
However names is defined as the following

*** Source Snippet Removed ***


There we see most of your problems.

The first problem is you are trying to use arrays of characters to represent text. In C++, we represent text with instances of std::string. The second problem is that you're using an array with some "maximum size" to represent a set of things that clearly doesn't have a natural restriction on its size. In C++, we represent sequences of unknown length with a standard library container, such as std::vector. The third problem is that you are passing two parallel structures of data to represent the students. In C++, we create structured data to represent a single thing, and make a container of structures, instead of several parallel containers.

Thus, we would do something like

struct Student {  std::string name;  // The number of grades per student *does* appear to be a specific value  // for a good reason: that's how many subjects are being taught to the class.  // Of course, you might decide that this restriction is not a useful one,  // and use a container here as well.  int grades[NUM_GRADES];};void printDatabase(const std::vector<Student>& students) {  // ...}


But that simply addresses some of your problems, and not your question. :)

Quote:I can not remember how your suppose to pass the char array into the function.


You are, in fact, doing it correctly. You have other errors (which you would have to fix even with modern C++ tools), as follows:

1) "NUM_STUDENTS" is mis-typed in one place.
2) To index into a multi-dimensional array, you must index each dimension separately: arr[j][k], not arr[j, k]. The latter means the same thing (in this case) as arr[k], which just gives you an array. (Passing an array to the operator<< overload for std::cout causes it to decay to a pointer, so you end up printing the pointer value.) If you don't understand why that is, read this.
3) (Possibly) Ensure that you are either 'using namespace std;', or explicitly qualify things from the std namespace with 'std::'. Also, note that 'setw' comes from <iomanip>, not <iostream>.

Quote:Original post by DevFred
The initializer on the right does not fit the data type on the left.


You are correct, but strangely, this doesn't seem to matter to the compiler. I assume that nested braces are more or less ignored for aggregate initializers.
I think one of you problem resides here :
for ( int i = 0; i < NUM_SUDENTS; i++ ) {      cout << setw( 10 ) << names;&lt;– Declared as 2d array, but you are trying to access it as 1d<br><br><br><br><span class="cpp-keyword">for</span> ( <span class="cpp-keyword">int</span> j = <span class="cpp-number">0</span>; j &lt; NUM_GRADES; j++ )<br>         cout &lt;&lt; setw( <span class="cpp-number">4</span> ) &lt;&lt; a[ i, j ]; &lt;– might not <span class="cpp-keyword">do</span> what you want it to <span class="cpp-keyword">do</span><br><br>}<br><br></pre></div><!–ENDSCRIPT–><br><br>
Our whole life is a opengl application.
The second one is a problem. The first is not, because the char[] decays to a char*, and passing a char* to std::cout::operator<< is a sensible thing to do (it interprets the pointed-at sequence of characters as a C string).

This topic is closed to new replies.

Advertisement