2009-09-20 9 views
1

콘솔 창에서 모든 출력을 네 개의 별도 필드로 채 웁니다. 모두 균등하게 간격을 둡니다. 그건 -콘솔에 서식 지정, C++

코드 위
out << left << setw(24) << "Name" << "Location" 
<< right << setw(20) << "Acres " << "Rating" << endl; 

out << left << setw(24) << "----" << "--------" 
<< right << setw(20) << "----- " << "------" << endl; 

while (current_node) 
    { 
     out << left << setw(24) << current_node->item.getName() // equivalent tabs dont work? 
      << current_node->item.getLocation() << right << setw(20) 
      << current_node->item.getAcres()   
      << " " << current_node->item.getRating() 
      << endl; 

     current_node = current_node->nextByName; 
    } 

어떤 이유로 setw가 (N)과 동일한 값 ...

+0

out << left << setw (24) << "Location"<< "Location" << 오른쪽 << setw (20) << "Acres"<< "Rating"<< endl; \t out << left << setw (24) << "----"<< "--------" << 오른쪽 << setw (20) << "---- - "<<"------ "<< endl; 동안 (current_node) 밖으로 \t \t \t { << 왼쪽 << setw (24) << current_node-> item.getName는() // 등가 탭 작동 해달라고? \t \t \t << current_node-> item.getLocation() << 오른쪽 << setw (20) \t \t << current_node-> item.getAcres() \t \t \t \t \t << ''<< current_node- > item.getRating() \t \t \t << endl; \t \t current_node = current_node-> nextByName; \t} – user40120

+0

램프 쉐이드, 읽기 : http://stackoverflow.com/editing-help – avakar

+2

@avakar 필자는 주석에서 여러 줄의 코드 형식이 무시된다고 생각합니다. –

답변

3

setw() 매니퓰레이터 만 다음 출력 필드에 영향이있는 경우 모두 균일하지 공간 수행 '끈적이지 않아'. 따라서 각 출력 필드에 대해 원하는 것을 지정해야합니다. 한 번만 변경하지 않고 다음 출력 항목 각각에 대해 작동 할 것을 기대해야합니다. 안전하게 입력,하지만 정신 안전하지 않은 - I/O를

cout << left << setw(24) << "Name" << setw(24) << "Location" ; 
cout << right << setw(20)<< "Acres" << setw(20) << "Rating" << endl; 

cout << left << setw(24) << "----" << setw(24) << "--------" ; 
cout << right << setw(20) << "-----" << setw(20) << "------" << endl; 

while (current_node) 
    { 
     cout << left << setw(24) << current_node->item.getName() 
        << setw(24) << current_node->item.getLocation() 
      << right 
        << setw(20) << current_node->item.getAcres() 
        << setw(20) << current_node->item.getRating() 
      << endl; 

     current_node = current_node->nextByName; 
    } 

C++ 스트림 :

난 당신이 뭔가를하려는 생각합니다.