C++ 数组结构未正确打印

C++ 数组结构未正确打印,c++,C++,我的数组结构无法正确打印。有人能帮我吗?`我必须打印学生成绩数据的数组结构。代码似乎可以工作,但我不知道打印数组结构时发生了什么。看起来calcAvg函数由于某种原因不能正常工作 代码: cis_students.txt: Robert Smallwood Mary Stevens Sally Moore John Perkins Connor Cousins William Laws Renee Rivers Thomas Carver Donna Smith 输出: Student 1:

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

代码:

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

sum
定义为

double sum;
但是之前没有初始化

sum += grades[student][a];
因此,操作开始时的sum值未知。可能是零。可能是十亿

解决方案:初始化总和

double sum = 0;
除此之外,
double calcHigh(双年级[][6],国际学生)

student-1
几乎肯定不是要计算其分数的
学生。特别糟糕的是,学生0将根据
分数[-1]
进行计算,该分数不存在并调用未定义的行为

解决方案

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

calcLow
也有同样的问题。

在从数组[-1]开始的每个函数中,在C/C++中,都不会出现异常,它只会将内存中的内容放在数组之前

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

未来的建议:不要在循环中使用“幻数”,将它们声明为一个变量,然后您可以在一个地方更改它,这样就不太可能出现错误提示行为

使用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;
  }
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
双calcAvg(双[][6],整数);
双calcLow(双[][6],int);
双卡奇(双[][6],整数);
结构studInfo
{
字符串fname;
字符串名称;
双低;
双高;
双倍平均;
} ;
int main()
{
ifstream输入;
ifstreaminput2;
双分[9][6];
字符串名;
字符串lastName;
整数计数=0;
struct studInfo学生[9];
打开(“cis_testGrades.txt”);
而(!input.eof())
{
对于(int a=0;a<9;a++)
{
请您的问题提供一个.Off-topic:这可能是错误:
while(!input.eof())
double sum = 0;
if (grades[student-1][a] >= high)
if (grades[student-1][a] >= high)
#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;
  }