数组结构无法正确打印
Array Struct Not Printing Correctly
我的数组结构无法正确打印。有人可以帮我吗?该代码似乎有效,但是我不知道它打印阵列结构时发生了什么。看起来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
在您从数组开始的每个功能中,在C/C 中,您不会得到例外,它只会在数组之前记忆中的内容。p>将[Student-1]更改为[student],然后初始化总和为0。
对未来的建议:不要在循环中使用"魔术数",将它们声明为变量,然后您可以在一个地方更改它,较小的错误提示行为。
与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;
}
in
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
。这特别不好的是学生0将在grades[-1]
上计算并不存在并调用不确定的行为。
解决方案
if (grades[student-1][a] >= high)
calcLow
有相同的问题。
相关文章:
- 如何循环打印顶点结构
- 如何将结构插入到集合中并打印集合的成员
- C++ 打印包含数据结构的数组
- 从 STL 优先级队列C++ (QtCreator) 打印结构
- 如何在 c++ 中打印结构
- 使用泛型函数调用打印结构的所有嵌套成员变量
- 矢量的打印结构元素
- 按字母顺序打印结构输入C
- 在C++中打印结构
- 访问程序中的符号表以打印结构的字段名称
- 如何打印结构的每个成员
- 如何在循环中打印结构中的数据
- GDB 打印结构的字段
- 如何打印结构矢量?它不是在保存数据吗
- 如何让 GDB 仅打印结构的特定成员的内容
- 打印结构体的向量时的奇怪行为
- LLDB调试器无法打印结构内容
- 使用迭代器从列表中打印结构体值
- 正在打印结构中的数组
- 如何在c++中打印结构数组中的所有内容