-4

My Array struct won't print correctly. Can someone please help me?`I have to print an array struct of student grades data. The code seems to work, but I don't know what is going on when it prints the array struct. And it looks like the calcAvg function isn't functioning properly for some reason.

Code:

    #include <iostream>
    #include <fstream>
    #include <cctype>
    #include <cstring>
    #include <iomanip>

    using namespace std;

    double calcAvg(double [][6], int);
    double calcLow(double [][6], int);
    double calcHigh(double [][6], int);

    struct studInfo{
        char fname[10];
        char lname[10];
        double low;
        double high;
        double average;
    };

    int main() {


        ifstream input;
        ifstream input2;
        double scores [9][6];
        char firstName[10];
        char lastName[10];
        int count = 0;


        struct studInfo students[9];

        input.open("cis_testGrades.txt");

        while (!input.eof()){
            for(int a = 0; a < 9; a++){
                cout << "Student " << a+1 << ": ";
                for(int b = 0; b < 6; b++){
                    input >> scores[a][b];
                    cout << scores[a][b] << " ";
                }
                cout << " " << endl;
            }
        }
        input.close();

        /*cout << calcAvg(scores, 9) << endl;
        cout << calcHigh(scores, 9) << endl;
        cout << calcLow(scores, 9) << endl;*/

        input2.open("cis_students.txt");

        while (!input2.eof()){

            input2 >> firstName;
            //firstName >> students[count].fname;
            strcpy(students[count].fname, firstName);

            input2 >> lastName;
            //lastName >> students[count].lname;
            strcpy(students[count].lname, lastName);

            students[count].low = calcLow(scores, count);

            students[count].high = calcHigh(scores, count);

            students[count].average = calcAvg(scores, count);

            count++;

        }

        input2.close();

        for(int a = 0; a < 9; a++)
            cout << students[a].fname << " " << students[a].lname << " " << students[a].low << " " << students[a].high << " " << students[a].average << endl;

        return 0;
    }

    double calcAvg(double grades[9][6], int student){

        double average;
        double sum;

        for(int a = 0; a < 6; a++)
            sum += grades[student][a];

        average = sum/6;

        return average;


    }

    double calcHigh (double grades[][6], int student){

        double high = 0;

        for(int a = 0; a < 6; a++) {
            if (grades[student-1][a] >= high)
                high = grades[student-1][a];
        }

        return high;

    }

    double calcLow (double grades[][6], int student){

        double low = 100;

        for(int a = 0; a < 6; a++) {
            if (grades[student-1][a] <= low)
                low = grades[student-1][a];
        }

        return low;

    }

cis_TestGrades.txt:

99 86 88 89 85 78 73 74 72 61 62 63 57 58 93 97 81 81 85 79 75 72 73 64 66 69 68 59 54 49 95 92 98 89
87 83 71 70 76 65 60 61 84 82 81 80 77 73 74 78 70 71 72 79

cis_students.txt:

Robert Smallwood
Mary Stevens
Sally Moore
John Perkins
Connor Cousins
William Laws
Renee Rivers
Thomas Carver
Donna Smith

Output:

Student 1: 99 86 88 89 85 78
Student 2: 73 74 72 61 62 63
Student 3: 57 58 93 97 81 81
Student 4: 85 79 75 72 73 64
Student 5: 66 69 68 59 54 49
Student 6: 95 92 98 89 87 83
Student 7: 71 70 76 65 60 61
Student 8: 84 82 81 80 77 73
Student 9: 74 78 70 71 72 79
Robert Smallwood 6.32404e-322 5.96342e+228 9.93903e+227
Mary Stevens 78 99 84
Sally Moore 61 74 90.1667
John Perkins 57 97 90.8333
Connor Cousins 64 85 75
William Laws 49 69 102.167
Renee Rivers 83 98 83.5
Thomas Carver 60 76 92.1667
Donna Smith 73 84 88

2 Answers2

0

In

double calcAvg(double grades[9][6], int student)

sum is defined as

double sum;

but is not initialized before

sum += grades[student][a];

so the value of sum at the beginning of the operation is unknown. Could be zero. Could be a billion.

Solution: Initialize sum.

double sum = 0;

In addition in double calcHigh (double grades[][6], int student)

if (grades[student-1][a] >= high)

student-1 is almost certainly not the student whose grades you want to compute. Where this fails particularly bad is student 0 who will be computed on grades[-1] which does not exist and invokes undefined behaviour.

Solution

if (grades[student-1][a] >= high)

calcLow has the same problem.

user4581301
  • 33,082
  • 7
  • 33
  • 54
0

In every function you are starting from array[-1], and in C/C++ you won't get an exception it will just take what is in memory before the array.

Change [student-1] to [student] and initialise sum to 0.

Advice for future: don't use 'magic numbers' in loops, declare them as a variable and then you can just change it in one place, less likely error prompt behaviour.

Compiled with VS 2015:

#include <iostream>
#include <fstream>
#include <cctype>
#include <cstring>
#include <iomanip>
#include <string>

using namespace std;

double calcAvg(double[][6], int);
double calcLow(double[][6], int);
double calcHigh(double[][6], int);

struct studInfo 
{
  string fname;
  string lname;
  double low;
  double high;
  double average;
} ;

int main() 
{
  ifstream input;
  ifstream input2;
  double scores[9][6];
  string firstName;
  string lastName;
  int count = 0;

  struct studInfo students[9];

  input.open("cis_testGrades.txt");

  while (!input.eof()) 
  {
    for (int a = 0; a < 9; a++) 
    {
      cout << "Student " << a + 1 << ": ";
      for (int b = 0; b < 6; b++) 
      {
        input >> scores[a][b];
        cout << scores[a][b] << " ";
      }
      cout << " " << endl;
    }
  }
  input.close();

  /*cout << calcAvg(scores, 9) << endl;
  cout << calcHigh(scores, 9) << endl;
  cout << calcLow(scores, 9) << endl;*/

  input2.open("cis_students.txt");

  while (!input2.eof()) 
  {
    input2 >> firstName;
    students[count].fname = firstName;
    //strcpy(students[count].fname, firstName);

    input2 >> lastName;
    students[count].lname = lastName;
    //strcpy(students[count].lname, lastName);

    students[count].low = calcLow(scores, count);

    students[count].high = calcHigh(scores, count);

    students[count].average = calcAvg(scores, count);

    count++;
  }

    input2.close();

  for (int a = 0; a < 9; a++)
  cout << students[a].fname << " " << students[a].lname << " " << students[a].low << " " << students[a].high << " " << students[a].average << endl;

  return 0;
}

  double calcAvg(double grades[][6], int student) 
  {
    double average;
    double sum = 0;

    for (int a = 0; a < 6; a++)
      sum += grades[student][a];

    average = sum / 6;

    return average;
  }

  double calcHigh(double grades[][6], int student) 
  {
    double high = 0;

    for (int a = 0; a < 6; a++) 
    {
      if (grades[student][a] >= high)
      high = grades[student][a];
    }
    return high;
  }

  double calcLow(double grades[][6], int student) 
  {
    double low = 100;

    for (int a = 0; a < 6; a++) 
    {
      if (grades[student][a] <= low)
      low = grades[student][a];
    }
    return low;
  }
  • This be bug: `while (!input.eof()) `. [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – user4581301 Dec 09 '16 at 02:04