我非常确定我的代码是可靠的,应该从我保存的记事本中获取所有内容,但似乎不是。我认为也可能是它可能会将所有内容都放入我的阵列但不能正确打印。谁能指出我的错误在哪里?我不知所措。
这是我的.txt文件中的文字:
Jason 10 15 20 25 18 20 26
萨曼莎15 18 29 16 26 20 23
Ravi 20 26 18 29 10 12 20
希拉17 20 15 26 18 25 12
Ankit 16 8 28 20 11 25 21
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void getData(ifstream& inf, string n[], double runData[][8], int count);
void calculateAverage(double runData[][8], int count);
void print(string n[], double runData[][8], int count);
int main()
{
string names[5];
double runData[6][8];
ifstream inFile;
inFile.open("Lab15Runner.txt");
if (!inFile)
{
cout << "Cannot open the input file: Lab15Runner.txt." << endl;
cout << "Program terminates!" << endl;
return 1;
}
cout << fixed << showpoint << setprecision(2);
getData(inFile, names, runData, 5);
calculateAverage(runData, 5);
print(names, runData, 5);
inFile.close();
return 0;
}
void getData(ifstream& inf, string n[], double runData[][8], int count)
{
for (int i = 0; i < count; i++)
{
inf >> n[i];
for (int j = 0; j < 8; j++)
inf >> runData[i][j];
runData[i][8] = 0.0;
}
}
void calculateAverage(double runData[][8], int count)
{
double sum;
for (int i = 0; i < count; i++)
{
sum = 0.0;
for (int j = 0; j < 8; j++)
sum = sum + runData[i][j];
runData[i][8] = sum / 7;
}
}
void print(string n[], double runData[][8], int count)
{
double sum = 0.0;
cout << left << setw(10) << "Name"
<< right << setw(8) << "Day 1"
<< setw(8) << "Day 2"
<< setw(8) << "Day 3"
<< setw(8) << "Day 4"
<< setw(8) << "Day 5"
<< setw(8) << "Day 6"
<< setw(8) << "Day 7"
<< setw(10) << "Average" << endl;
for (int i = 0; i < count; i++)
{
cout << left << setw(10) << n[i];
cout << right;
for (int j = 0; j < 9; j++)
cout << setw(8) << runData[i][j];
cout << setw(8) << runData[i][9];
}
}
答案 0 :(得分:0)
你有7个int值但你试着读8 [0-7]。当阵列中的最后一个位置是[7]时,你也试图写入位置[8]。
n[i] runData[i][j] --> [0] [1] [2] [3] [4] [5] [6] {Length = 7}
0 Jason 10 15 20 25 18 20 26
1 Samantha 15 18 29 16 26 20 23
2 Ravi 20 26 18 29 10 12 20
3 Sheila 17 20 15 26 18 25 12
4 Ankit 16 8 28 20 11 25 21
我更改了您的代码并在更改前评论了// CHANGED
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
void getData(ifstream& inf, string n[], double runData[][8], int count);
void calculateAverage(double runData[][8], int count);
void print(string n[], double runData[][8], int count);
int main()
{
string names[5];
double runData[6][8];
ifstream inFile;
inFile.open("Lab15Runner.txt");
if (!inFile)
{
cout << "Cannot open the input file: Lab15Runner.txt." << endl;
cout << "Program terminates!" << endl;
return 1;
}
cout << fixed << showpoint << setprecision(2);
getData(inFile, names, runData, 5);
calculateAverage(runData, 5);
print(names, runData, 5);
inFile.close();
return 0;
}
void getData(ifstream& inf, string n[], double runData[][8], int count)
{
for (int i = 0; i < count; i++)
{
inf >> n[i];
//CHANGED - reads all 7 values after the name
for (int j = 0; j < 7; j++) // Old --> for (int j = 0; j < 8; j++)
inf >> runData[i][j];
//CHANGED - set the last value (8th position) to 0.0
runData[i][7] = 0.0; //Old --> runData[i][8] = 0.0;
}
}
void calculateAverage(double runData[][8], int count)
{
double sum;
for (int i = 0; i < count; i++)
{
sum = 0.0;
//CHANGED - sum all 7 values
for (int j = 0; j < 7; j++) //Old --> for (int j = 0; j < 7; j++)
sum = sum + runData[i][j];
//CHANGED - update the 8th position
runData[i][7] = sum / 7; // Old --> runData[i][8] = sum / 7;
}
}
void print(string n[], double runData[][8], int count)
{
double sum = 0.0;
cout << left << setw(10) << "Name"
<< right << setw(8) << "Day 1"
<< setw(8) << "Day 2"
<< setw(8) << "Day 3"
<< setw(8) << "Day 4"
<< setw(8) << "Day 5"
<< setw(8) << "Day 6"
<< setw(8) << "Day 7"
<< setw(10) << "Average" << endl;
for (int i = 0; i < count; i++)
{
cout << left << setw(10) << n[i];
cout << right;
//CHANGED - Print all 7 values
for (int j = 0; j < 7; j++)// Old --> for (int j = 0; j < 9; j++)
cout << setw(8) << runData[i][j];
//CHANGED - print the average (average is at position [i][7], position [i][9] doesn't exist) and added endl at the end
cout << setw(8) << runData[i][7] << endl;// Old --> cout << setw(8) << runData[i][9];
}
}
它应该可以正常工作。