【发布时间】:2014-03-14 00:35:54
【问题描述】:
我是 C++ 新手,我一整天都在努力解决这个问题。我正在尝试读取一个包含未知数量行的数据文件(老师说不会超过 100 行)。每行包含 4 个整数,其值范围为 0-100。 4 列代表一个学期的学生考试成绩。每行/行代表一个学生的分数。每列代表 1 个测试。我要设置一个二维数组来读取分数。数据文件中的分数进入前 4 列,在第 5 列中为每个学生/行计算所有 4 次测试的平均值。由于我不知道文件中有多少学生/行,我将有第 0 行到第 n-1 行。在第 n 行,我将每个位置 row[0 到 4] 计算为其上方整列的平均值。在每列行底部计算的所有学生的平均成绩(第 n 行)和在第 5 列计算的所有四次测试的每个学生的平均成绩。在第 5 列的底部计算的每个学生的平均测试平均成绩(成绩[第 n 行] [5] ={第5列所有行的平均值}
我确信更广泛的知识库会非常有益,但这是一项家庭作业,所以我不得不尝试。我认为指针对于这项任务来说是最有益的理解。但是,我还没有到达那里。
这是我的第一次尝试:
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <sstream>
#include <cmath>
#include <vector>
using namespace std;
double grades [100][5] = {0};
int row = 0;
int column = 0;
int main() {
char line[100];
ifstream myfile("sturec.dat");
if(myfile){
while (getline(myfile, line)) {
if (line == char) {
//grades[row][column] =
cout << line << endl;
for (int i = 0; i <= line.length(); i++) {
if (line[i] != line[0]) && (i == " ") && (line[i+1] == char) {
column += 1;
}
else if (line[i] && line[i+1] && line[i+2] !== " ") {
grades[row][column] = {line[i] + line[i+1] + line[i+2]};
else if (line[i] && line[i+1] !== " " ) {
grades [row][1] = {line[i] + line[i+1]};
}
}
row += 1;
}
}
}
} 我放弃了,重新开始尝试创建一个向量向量来填充文件。我花了很长时间才弄清楚如何从文件中实际导入数据。最后我求助于:
#include //all the necessary libraries
using namespace std;
double grades[100][5] = {0}//the 2d array i had hoped to populate with the data from file
int main(){
ifstream myfile("filename");
rowCount = 0;
int t1, t2, t3, t4;
while(myfile >> t1 >> t2 >> t3 >> t4){
cout << t1 << " " << t2 << " " << t3 << " " << t4 << endl;
cout << "this is row 1 + : " << rowCount << endl;
//at this point i was just happy to have successfully read the file and printed the values.
rowCount ++;
}
for(int i = 0; i < 4; i++){
grades[rowCount][i]// this is where i got lost i tried multiple different things in attempt to populate "grades" by trying to create temp arrays to hold the values of t1,2,3,4 in order to parse them and place them in "grades", but to no avail. Any direction would be appreciated.
}
只是为了展示我的一些不同方法,我将发布我所拥有的类似代码的略有不同的版本。
``
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
double grades[100][5] = {0};
int main() {
ifstream myfile("sturec.dat");
int rowCount = 0;
int tempArray[100][4] = {0};
char test [4] = {0};
int i = 0;
while (myfile >> tempArray[rowCount][i]) {
cout << rowCount << endl << " " << i << endl;
cout << "temp array: " << tempArray<< endl;
while(i < 4){
i++;
rowCount++;
}
}
/*for (int c = 0; c <= rowCount; c++) {
for (int r = 0; r <= i; r++) {
grades[rowCount][i] = (tempArray[r][c]);
}
}
cout<< tempArray << endl << grades << endl;
*/
}
/*double final;
while (myfile >> grades[rowCount][test]) {
//cout << t1 << " " << t2 << " " << t3 << " " << t4 << endl;
cout << grades << endl;
cout << rowCount << endl;
//cout << grades[rowCount][]
rowCount ++;
}
}
*/
下一个
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
double grades[100][5] = {0};
int main() {
ifstream myfile("sturec.dat");
int rowCount = 0;
int tempArray[100] = {0};
int t1, t2, t3, t4;
while (myfile >> t1 >> t2 >> t3 >> t4) {
cout << t1 << " " << t2 << " " << t3 << " " << t4 << endl;
int test [4] = {0};
for (int i = 0; (i < sizeof(test) - 1); i++) {
grades[rowCount][i] = {tempArray};
}
}
double final;
while (myfile >> grades[rowCount][i]) {
cout << grades << endl;
cout << rowCount << endl;
//cout << grades[rowCount][]
rowCount ++;
}
vector < vector <int> > grades(100);
//vector <int> rows(4/*,0*/); // assigns 4 columns to rows vector with value of zero
//rows.assign(5,0);
int row = 0;
myfile.open("sturec.dat", ios::in); //opens file
if (myfile.is_open()) {
cout << "file opened" << endl;
string line;
vector<string> myLines;
while (getline(myfile, line)) { //gets lines using myfile and puts them in line
myLines.push_back(line);
cout << "string line contains: " << line << endl;
for (int columns = 0; columns <= 4 /*sizeof(rows)*/; columns ++) {
myfile >> grades[row][columns]; cout << "2" << endl;
}
row += 1;
}
}
else cout << "cannot open file" << endl;
myfile.close(); cout << "closed file" << endl;
return 0;
//cout << grades;
}
最后一个:
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;
int main() {
ifstream myfile;
vector < vector <int> > grades(100);
//vector <int> rows(4/*,0*/); // assigns 4 columns to rows vector with value of zero
//rows.assign(5,0);
int row = 0;
myfile.open("sturec.dat", ios::in); //opens file
if (myfile.is_open()) {
cout << "1" << endl;
cout << "file opened" << endl;
string line;
vector<string> myLines;
while (getline(myfile, line)) { //gets lines using myfile and puts them in line
myLines.push_back(line);
cout << "string line contains: " << line << endl;
for (int columns = 0; columns <= 4 /*sizeof(rows)*/; columns ++) {
myfile >> grades[row][columns]; cout << "2" << endl;
}
row += 1;
}
}
else cout << "cannot open file" << endl;
myfile.close(); cout << "closed file" << endl;
return 0;
//cout << grades;
} 这实际上让我得到了文件的第一行,但我无法让这个错误消失: 运行命令:第 1 行:13531 分段错误:11 ./"$2" "${@:3}"
【问题讨论】:
-
vector
> 等级(100,vector (5,0)); -
C++ 不是一种非常适合试错语法的语言
-
"// 这就是我迷路的地方" - 听起来你只是想要
grades[rowcount][0] = t1; grades[rowcount][1] = t2; grades[rowcount][2] = t3; grades[rowcount][3] = t4;,并且将该代码放在while循环中而不是在它之后,并增加rowcount.
标签: c++ vector multidimensional-array fstream filereader