2014-03-14 78 views
0

我是新来的C++和ive一直试图围绕这一整天。我试图读取包含未知数量行的数据文件(老师说不会超过100行)。每行包含4个整数,其值在0-100之间。这4列代表一学期的学生考试成绩。每行/每行代表一名学生的成绩。而每列代表1个测试。我将设置一个2D数组来读取分数。数据文件的分数进入前四列,每列学生/行在第5列中计算的所有4个测试的平均值。由于我不知道文件中有多少个学生/行,因此我将有0行到n-1行。在第n行,我计算每个位置行[0到4]作为整个列上的平均值。在第5列底部计算的所有学生的平均成绩和在第5列计算的所有四个成绩的每个学生的平均成绩。第5列底部计算的每个学生的平均成绩平均成绩(成绩[第[第n行] [5] = {第5列中所有行的平均值}将2d数组的实际值复制到另一个2d数组C++

我相信一个更广泛的知识库将是非常有益的,但这是一项家庭作业,所以我不得不尝试,我认为指针会是最有利的。首先要了解此作业;不过,我只是还没有得到尚未有

这是我第一次尝试:

#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}”

+1

矢量<矢量>等级(100,矢量(5,0 )); – camino

+1

C++不是一种非常适合反复试验语法的语言 –

+0

“//这是我迷失的地方” - 听起来你只是想要[成绩[rowcount] [0] = t1;等级[rowcount] [1] = t2;等级[rowcount] [2] = t3;等级[rowcount] [3] = t4;',并且将该代码放在while循环中,而不是在它之后,并增加'rowcount'。 –

回答

0
vector < vector <int> > grades(100); 

初始化含100个vector<int> s的矢量大小为0。为避免在最后一个例子分段错误,您应该初始化这个作为

vector < vector <int> > grades(100,vector<int>(5,0)); 

或替换的东西像myfile >> grades[row][columns];

int tmp; 
myfile >> tmp; 
grades[row].push_back(tmp); 
+0

我明白了。谢谢! – user3414683