Two-Dimensional Char Array

Hello, I am having a problem writing this program. I'm supposed to make a two-dimensional array of characters to hold the five student names.

Like so:

1
2
3
const int NUM_STUDENTS = 10;
const int STRING_SIZE = 10;
char names[NUM_STUDENTS][STRING_SIZE];


But the user is supposed to enter the names, which is what I am having trouble doing. Can anyone please explain to me how to go about doing so? Thanks
Are you asking how to input data into the array?

1
2
3
4
5
	for (int i = 0; i < NUM_STUDENTS; i++)
	{
		cout << "enter student name: ";
		cin >> names [i];
	}
Just like that? What about the STRING_SIZE part? I don't need to do anything with it?
Oh, I suppose it would be a good idea to make sure the read doesn't overrun your array.

1
2
istream& get ( char* s, streamsize n );
istream& get ( char* s, streamsize n, char delim );


Either of these should work for you.
I don't know if I'm just confusing myself or what... This is the problem that I am supposed to do:

1
2
3
4
Write a program that uses a two-dimensional array of characters to hold the five stu-
dent names, a single-dimensional array of five characters to hold the five student's let-
ter grades, and five single-dimensional arrays of four doubles to hold each student's
set of test scores.


The program I wrote sort of worked but since it's always good to get the input of others, I want to make sure I am understanding everything clearly. So, I'll do a sample model for the problem above:

1
2
3
4
5
6
7
8
9
10
11
const int NUM_STUDENTS = 5,   //number of students
              STRING_SIZE = 10;     //maximum size of each string
              GRADES = 4;               //number of test scores for each student
char names[NUM_STUDENTS][STRING_SIZE];  //The names of five students with maximum 
                                        //of 10 characters for each name entered
char letter_grade[NUM_STUDENTS];         //the letter grades for the five students
double test1[GRADES],            //
            test2[GRADES],           //
            test3[GRADES],           //     the test grades for the five students. not sure if this 
            test4[GRADES],           //     part is correct.
            test5[GRADES];          // 
Last edited on
would this be right so far?
It looks OK, except that 10 characters isn't much space to hold a name. You might want to bump that up; memory's cheap these days.

And, while your test1/2/3/4/5 arrays will work, they're poorly named. I'd call them "student1grades, etc. or something like that.

Apart from these minor issues, this looks like you can make it work.
why not

double test_grades[NUM_STUDENTS][GRADES];

to parallel what you're doing with names and letters_grades?


Oops -- didn't read back up the thread far enough -- Sorry!
Last edited on
That's not what the instructions call for.
Because it asked for five single-dimensional arrays of four doubles to hold each student's
set of test scores.
Then the second part of the instruction is:

1
2
3
The program should allow the user to enter each student's name and his or her four
test scores. It should then calculate and display each student's average test score and a 
letter grade based on the average.


The only part that is giving me trouble is entering the student's names. Would I use a nested for loop or something?

1
2
3
4
5
6
7
for(int count = 0; count < NUM_STUEDNTS; count++)
{
      for(int row = 0; row < STRING_SIZE; row++)
      {
            cin >> names[count][row];    //something like this??
      }
}


Or would it be something like:
 
cin.getline(names, STRING_SIZE);


Actually, somewhere in between. Using getline is probably the right approach, but you need to dereference names once (not twice). And a single for loop should work fine.
This seems to work just fine :)

1
2
3
4
5
for(int count = 0; count < NUM_STUDENTS; count++)
	{
		cout << "\n Student " << (count + 1) << ":  ";
		cin.getline(names[count], STRING_SIZE);
	}
Bingo. You're making good progress. Let us know if you need any more help.
Topic archived. No new replies allowed.