Beginner programming array question

I'm in the middle of trying to write a program for my C++ class and am stuck on a certain part. I need to write a program, using a two dimensional array, where I am creating a student data base. I need to enter 10 students names with 5 grades per student after which I have to display each students average as well as the class average. What I'm having a problem is is with the array that stores the grades. It appears that each time I enter a new students grades, it over writes the previous students grades. So what prints out is that every student has the same exact grades. How do I get it so that I'm not over writing the previous grades. Here's what I have wriiten for code so far.


//HOMEWORK 5. C++ PROGRAMMING DOUGLAS GREIG

#include<iostream>
#include<iomanip>


using namespace std;

int main(){

     //two dimensional array of charachters
     
     const int numNames = 2;                        //max number of names entered
     const int maxltr = 80;
     const int numgrades = 5;
     const int maxltrg = 10;
   
     char names[numNames][maxltr] = {0};                     //sets the array "names"
    char grades[numgrades][maxltrg] = {0};                  //sets the array "grades"
   

     //get the names and grades

     for(int i = 0; i < numNames; i++){
          cout << "ENTER THE STUDENTS NAME: ";
          cin.getline(names[i], maxltr);
          for(int k = 0; k < numgrades; k++){
               cout << "ENTER THE STUDENTS GRADES: ";
               cin.getline(grades[k], maxltrg);
          }

                   
         
         
     }

     //calulate the students average





     //print them out

     cout << "\n\n\n";

     cout << "    NAME   GRADES                    AVERAGE" << endl;
     cout << "--------------------------------------------" << endl;

     for(i = 0; i < numNames; i++){
             cout << setw(8) << names[i];
          for(int k = 0;k < numgrades; k++){
               cout << setw(5) << grades[k];
          }          
     cout << endl;
     }

     cout << "\n\n\n";

     int average = 0;
     int total = 0;

     total = ;

     average = (total / numgrades);


     cout << names[1] << average << endl;

     
     return 0;
}
douggrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

bobbit31Commented:
i won't finish the whole thing for you, but take a look at what i'm doing here:

#include<iostream>
#include<iomanip>

using namespace std;

int main(){

     //two dimensional array of charachters

     const int numNames = 2;                        //max number of names entered
     const int maxltr = 80;
     const int numgrades = 5;
     const int maxltrg = 10;

     char names[numNames][maxltr] = {0};                     //sets the array "names"
     char grades[numNames*numgrades][maxltrg] = {0};         //sets the array "grades"

     //get the names and grades

     for(int i = 1; i <= numNames; i++){
          cout << "ENTER THE STUDENTS NAME: ";
          cin.getline(names[i-1], maxltr);
          for(int k=(i*numgrades)-numgrades; k < (i*numgrades); k++){
               cout << "ENTER THE STUDENTS GRADES: ";
               cin.getline(grades[k], maxltrg);
          }
     }


     //calulate the students average





     //print them out

     cout << "\n\n\n";

     cout << "    NAME   GRADES                    AVERAGE" << endl;
     cout << "--------------------------------------------" << endl;

     for(int i = 1; i <= numNames; i++){
             cout << setw(8) << names[i-1];
          for(int k=(i*numgrades)-numgrades;k < (i*numgrades); k++){
               cout << setw(5) << grades[k];
          }
     cout << endl;
     }

     cout << "\n\n\n";

     int average = 0;
     int total = 0;

     // total = ;

     average = (total / numgrades);


     cout << names[1] << average << endl;


     return 0;
}
0
bobbit31Commented:
basically, the way you had it, your grades array was an array of characters (up to maxltrg in size) that had only numgrades empty spaces. (that's why you were overwriting the values)

i changed it so that grades had numNames*numgrades empty spaces:

grades[0]
grades[1]
grades[2]
grades[3]
grades[4]

are the grades for name 0

grades[5]
grades[6]
grades[7]
grades[8]
grades[9]

are for names 1

actually, you'd be better off creating a Student class and then just having an array of students... but assuming this is a homework problem, i tried to keep to the subject at hand.

good luck
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
FrancozCommented:
The problem is with your second for loop to compute avarage,

for(int k = 0;k < numgrades; k++)

Here you've reinitializing the variable used to get the grade array to zero. This is wrong. Instead of that you should compute the index in htis way

for (int i = 0 ; i < numNames ; i++)
{
 //do with name array

 for(int k = i*5; k < (i+1)*5 ; k++)
 {
  //do with grades array
 }
}

I found one more mistake that you're not using the 0th elt. present in Names array since you've started index with 1.

See whether my understandings are correct or not.
0
bcladdCommented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Answered: Points to bobbit31

Please leave any comments here within the next seven days. Experts: Silence
means you don't care.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

-bcl (bcladd)
EE Cleanup Volunteer
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.