Hi everyone. I'm a bit of a noob at C++, I took the first level of this class 2 years ago and I'm tying to remember everything. I'm having a problem with a program I'm trying to write. I need to average the grades of each student and then print them in a column. I have to print the grade book (with the average) as a function.

The output should look similar to:

Name Grades Average
Baker 89 77 91 85

Here's what I have so far:

#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <math.h>

using namespace std; 

ifstream inFile; 

//Function Prototypes.
void get_Info (string students[], int grades[][3]);
void Prt_Gradebook (string students[], int grades[][3]);

int main ()
{   
    string students[5];                //Array of students.
    int grades[5][3];                 //2D array for grades.
    get_Info (students, grades);      //
    Prt_Gradebook (students, grades);//Print the gradebook and the avg for each student.
    
    
    system ("PAUSE");
    return 0;
}

void get_Info (string students[], int grades[][3])
{
     inFile.open("QuizData.txt");
     for (int r=0; r < 5; r++)
     { inFile >> students[r];
       for (int c=0; c < 3; c++)
       inFile >> grades[r][c];
     }
}

void Prt_Gradebook (string students[], int grades[][3])
{
     for (int r=0; r < 5; r++)
     {
         cout << students[r]<< setw(5)<< " ";
                 
         for (int c=0; c < 3; c++)
         {  
             cout << setw(5) << grades[r][c]<< " ";
     
          for (int r=0; r < 5; r++)
          {   
              int sum=0;
              int avg;
              
              sum += grades[r][c];
              
          }
              
         
         }
         cout << endl;
     }
}

I have it printing the names and grades arrays fine, it's when I try to sum, average and then put the averages into a new column that I get terribly confused. I took out what I had that was truly messed up.

Help please!

Well, you only want the average to print once and that's after you have shown all the grades. If I'm reading this correctly, you have 5 students, each with 3 grades.
Something of this nature should take care of it:

for each student from 0 to 4 {  //begin student display for
  declare a sum variable, initialize to zero (0)
  declare an average variable, initialize to zero (0)

  display student name

  for each grade from 0 to 2 { //begin grade display for
    display the grade

    add the grade to the sum
  } //end grade display for

  calculate the average from the sum
  display the average

} //end student display for

I suspect that part of the issue you are having is how to calculate and display the sum and average. The problem is that you have the variables for those values declared in the wrong scopes. The thing that you have to remember is that each for loop has its own scope independent of the others. The scopes may overlap, but they are independent.

If you have 2 nested scopes (for example, 2 nested for loops), the inner scope can see all of the outer scope, but the outer scope can not see the inner.

for( ; ; ) {          //begin outer for loop scope
  int exampleVar1 = 0;

  for ( ; ; ) {       //begin inner for loop scope
    int exampleVar2 = 0;
    exampleVar1 = 3;  //legal, because exampleVar1 is from outer for's scope
    exampleVar2 = 6;  //also legal, exampleVar2 is local to inner for's scope

  }                   //end inner for loop scope

  exampleVar1 = 9;    //legal, exampleVar1 is local to outer for's scope

  exampleVar2 = 12;   //NOT LEGAL, exampleVar2 is out of scope, it was destroyed at end of inner for

}                     //end outer for loop scope
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.