2011-03-02 50 views
0

我想從txt文件輸入數據。 該文件包含二維數組[5] [5] 如何打印出我想要的任何值? 我不希望打印出整個5點* 5的數據我想從二維數組打印出一些值C++

#include <iostream> 

#include <fstream> 
#include <string> 


using namespace std; 



int main() 
{ 

    double distance[5][5] ; 
    string line; 

    ifstream ratefile; 
        ratefile.open("a.txt"); 
    ofstream file; 
    if (ratefile.is_open()) 
     { 
    while (! ratefile.eof()) 
    { 
     getline (ratefile,line); 
    ratefile.getline(distance, 25, '*'); 

    cout << "\nDistance [0][0]" << ": " << distance[0][0]; 
    cout << "\nDistance [0][1]" << ": " << distance[0][1]; 
    cout << "\nDistance [0][2]" << ": " << distance[0][2]; 
    cout << "\nDistance [0][3]" << ": " << distance[0][3]; 
    cout << "\nDistance [1][0]" << ": " << distance[1][0]; 
    cout << "\nDistance [1][1]" << ": " << distance[1][1]; 
    cout << "\nDistance [1][2]" << ": " << distance[1][2]; 
    cout << "\nDistance [1][3]" << ": " << distance[1][3]; 

    cout << endl; 


    cin.get(); 



    return 0; 
} 
+0

你錯過一些線路中的碼? – Fox32 2011-03-02 13:11:30

+0

作爲一名程序員,如果我想打印出我想要的值,我會打印出我想要的值。如果你想有人回答這個問題,你需要指定問題是什麼...... – Lindydancer 2011-03-02 13:12:40

+0

這是家庭作業嗎? – Nav 2011-03-02 13:25:57

回答

0

還有就是代碼所缺少的一部分,但你要打印你想要的值。只需刪除不想打印的行。

當然,你也可以使用變量:

int x = 2,y = 2; 
cout << endl << "Distance [" << x << "][" << y << "] : " << distance[x][y]; 
1

如果您只想輸出一個值和用戶應該能夠選擇一個值,你可以做這樣的事情:

int x, y; 

cin >> x; 
cin >> y; 

cout << "\nDistance [" << x << "][" << y << "]" << ": " << distance[x][y]; 

但是,你應該檢查,如果用戶輸入有效的數字(0 < = X < 4和0 < = Y < 4)