2016-11-01 27 views
0

我正在寫這個代碼,我用一個文件中的信息填充2維數組。這裏的文件:將元素添加到2維數組C++

5 

Franks,Tom 2 3 8 3 6 3 5 

Gates,Bill 8 8 3 0 8 2 0 

Jordan,Michael 9 10 4 7 0 0 0 

Bush,George 5 6 5 6 5 6 5 

Heinke,Lonnie 7 3 8 7 2 5 7 

現在的數字數組中去:data[50][8]

我還總計了我所做的每一行中的所有數字。我想將這個總和添加到數據數組中,所以它看起來像2 3 8 3 6 5 3 30。我該怎麼做呢? 這裏是我的代碼,如果你想看到它:

int main() 
{ 

    ifstream fin; 
    char ch; 
    int data[50][8]; 
    string names[50]; 

    fin.open("empdata.txt"); 

    int sum = 0; 
    int numOfNames; 
    fin >> numOfNames; 

    for (int i = 0; i < numOfNames; i++) { 

     fin >> names[i]; 

     for (int j = 0; j < 7; j++) { 
      fin >> data[i][j]; 
     } 
      } 

     for (int i = 0; i < 5; i++) 
     { 

      for (int j = 0; j < 7; j++) 
      { 
       sum += data[i][j]; 

      } 
      cout << sum << endl; 
      sum = 0; 
     } 
} 

這裏是新的代碼,C650幫我。這不是現在輸出任何東西: INT主要(){

ifstream fin; 
char ch; 
int data[50][8]; 
string names[50]; 

fin.open("empdata.txt"); 

int sum = 0; 
int numOfNames; 
fin >> numOfNames; 

for (int i = 0; i < numOfNames; i++) { 

    fin >> names[i]; 

    data[i][7] = 0; 

    for (int j = 0; j < 7; j++) { 
     fin >> data[i][j]; 
     data[i][7] += data[i][j]; 
    } 
} 

for (int i = 0; i < numOfNames; i++) 
{ 
    cout << data[i][7] << endl; 

} 



system("pause"); 
return 0; 

}

+0

請格式化文件... – Charles

+0

我剛剛做了。希望有幫助 – Ralf

+0

我建議你使用STL。一個'std :: vector'可能真的會幫助你! – Charles

回答

1

下面好像你正在嘗試做的。我將在您提供的代碼中指出缺陷。

這不是最好的辦法,但根據OP有一些限制,比如不能使用STL。

/* 1 */ #include <fstream> 
/* 2 */ #include <iostream> 
/* 3 */ #include <string> 
/* 4 */ 
/* 5 */ using namespace std; 
/* 6 */ int main() 
/* 7 */ { 
/* 8 */ 
/* 9 */  ifstream fin; 
/* 10 */  int data[50][8]; 
/* 11 */  string names[50]; 
/* 12 */ 
/* 13 */  fin.open("test.txt"); 
/* 14 */ 
/* 15 */  int numOfNames; 
/* 16 */  fin >> numOfNames; 
/* 17 */ 
/* 18 */  for (int i = 0; i < numOfNames; i++) { 
/* 19 */ 
/* 20 */   fin >> names[i]; 
/* 21 */ 
/* 22 */   data[i][7] = 0; /* use last spot in array for sum, set to 0. */ 
/* 23 */   for (int j = 0; j < 7; j++) { 
/* 24 */    fin >> data[i][j]; 
/* 25 */    data[i][7] += data[i][j]; 
/* 26 */   } 
/* 27 */  } 
/* 28 */ 
/* 29 */  for (int i = 0; i < numOfNames; i++) 
/* 30 */  { 
/* 31 */   cout << data[i][7] << endl; /* add each element to the sum here*/ 
/* 32 */  } 
/* 33 */  return 0; 
/* 34 */ } 

OP有問題,沒有輸出產生。 OP,您必須確保您的輸入文件具有正確的名稱...

+0

好吧,似乎它會工作,但它現在不輸出任何東西。這裏是代碼 – Ralf

+0

data [i] [7] + = data [i] [j]; \t \t} \t} \t對(INT I = 0; I Ralf

+0

ohhh,沒有過去的權利,舉起我只是編輯我的頂級問題 – Ralf

0

正如我在其他類似帖子中回答的:使用結構來建模記錄。

struct Record 
{ 
    std::string last_name; 
    std::string first_name; 
    unsigned int grades[MAXIMUM_GRADES]; 
}; 

這可以讓你有混合類型的數組:

Record student_info[MAXIMUM_STUDENTS]; 

爲了從文件中讀取更加容易,你可以重載operator>>裏面的結構:

struct Record 
{ 
    //... 
    friend std::istream& operator>>(std::istream& input, Record& r); 
}; 

std::istream& operator>>(std::istream& input, Record r) 
{ 
    std::getline(input, r.last_name, ','); 
    std::getline(input, r.first_name, ' '); 
    for (unsigned int i = 0; i < MAXIMUM_GRADES; ++i) 
    { 
    input >> r.grades[i]; 
    } 
    return input; 
} 

這簡化你的輸入迴路:

for (unsigned int i = 0; i < MAXIMUM_STUDENTS; ++i) 
{ 
    my_data_file >> student_info[i]; 
} 

太糟糕了,你不能使用std::vectorstd::vector非常適合從文件中讀取,尤其是當您不知道文件大小時。

+0

這個人還沒有關於矩陣他怎麼能理解結構? – Real73

+0

@ShahrairNazimReal:取決於指導課程和討論主題的順序。沒有必要在結構之前討論數組。數組之前可以討論結構。 –