Sign in to follow this  
toogreat4u

Passing a 2D array of characters to a function

Recommended Posts

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[ i ];
      
      for ( int j = 0; j < NUM_GRADES; j++ )
         cout << setw( 4 ) << a[ i, j ];

      cout << endl;

   } // end for

   cout << endl;

} // end printDatabase

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.

Share this post


Link to post
Share on other sites
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[s];
cout << "Name: " << stud.Name << endl;
cout << " Scores: ";

size_t tCnt = stud.TestScores.size();
for (size_t t = 0; t < tCnt; ++t)
{
cout << stud.TestScores[t] << " ";
}

cout << endl;
}
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I think one of you problem resides here :


for ( int i = 0; i < NUM_SUDENTS; i++ ) {
cout << setw( 10 ) << names[ i ];<-- Declared as 2d array, but you are trying to access it as 1d



for ( int j = 0; j < NUM_GRADES; j++ )
cout << setw( 4 ) << a[ i, j ]; <-- might not do what you want it to do

}



Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this