2014-03-14 76 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