2011-03-02 3 views
0

txt 파일에서 데이터를 입력하고 싶습니다. 파일에 2-d 어레이가 있습니다. [5] [5] 원하는 모든 값을 어떻게 출력합니까? 나는 전체 5 개 * 5 데이터2 차원 배열에서 일부 값을 출력하고 싶습니다. 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

+0

프로그래머가 원하는 값을 인쇄하고 싶으면 원하는 값을 인쇄합니다. 누군가가 질문에 대답하기를 원하면 문제가 무엇인지를 지정해야합니다. – Lindydancer

+0

이 숙제입니까? – Nav

답변

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)

관련 문제