阵列结构无法正确打印

时间:2016-12-08 23:03:22

标签: c++

我的数组结构将无法正确打印。有人可以帮助我吗?我必须打印学生成绩数据的数组结构。代码似乎工作,但我不知道它打印数组结构时发生了什么。并且看起来calcAvg函数由于某种原因无法正常运行。

代码:

    #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

输出:

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 个答案:

答案 0 :(得分:0)

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

sum定义为

double sum;

但未在

之前初始化
sum += grades[student][a];

所以操作开始时的sum值是未知的。可能是零。可能是十亿。

解决方案:初始化总和。

double sum = 0;

此外还有double calcHigh (double grades[][6], int student)

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

student-1几乎肯定不是您想要计算成绩的student。如果失败特别糟糕,那么将在grades[-1]上计算的学生0将不存在并调用未定义的行为。

解决方案

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

calcLow也有同样的问题。

答案 1 :(得分:0)

在每个函数中,你从数组[-1]开始,在C / C ++中,你不会得到一个例外,它只会占用数组之前的内存。

将[student-1]更改为[student]并将sum初始化为0。

对未来的建议:不要使用魔法数字&#39;在循环中,将它们声明为变量,然后您可以在一个地方更改它,不太可能出现错误提示行为。

使用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;
  }
相关问题