2016-05-06 7 views
0

私はプリントアウトするために、次のコードを持っている:プリントをテーブルのようにするにはどうしたらいいですか?

cout<<current->bookId<<"\t\t"<<current->bookName<<"\t\t"<<current->year<<"\t\t"; 
cout<<"Checked out by student "<<current->storedBy<<endl; 

この

BookId   BookName    Year   Status 
1000   Machine Learning    1997   Checked out by student 21000000 
1200   Data Mining    1991   Checked out by student 21000020 
1400   C++ How to Program    2005   Checked out by student 21000020 
1500   Pattern Recognition    2000   Checked out by student 21000000 

が、私はそのようにそれを作るために何をすべきように見えます:

BookId   BookName      Year   Status 
1000   Machine Learning    1997   Checked out by student 21000000 
1200   Data Mining      1991   Checked out by student 21000020 
1400   C++ How to Program    2005   Checked out by student 21000020 
1500   Pattern Recognition    2000   Checked out by student 21000000 

答えて

4

Set field widthstd::setwは何ですかあなたは探している。

使用例:

#include <iostream>  // std::cout, std::endl 
#include <iomanip>  // std::setw 

int main() { 
    std::cout << std::setw(10) << "test" << std::endl; 
    return 0; 
} 

パラメータが10であるので、std::setwは、10日、11日、12日スクリーンの13文字を"test"を表示するため。あなたのケースでは

、あなたはこれまでのルーチンと同様のを使用して、いくつかの試行錯誤であなたが望むものを達成します:

std::cout << std::setw(10) << "col1" << std::setw(5) << "col2" << std::endl; 
+1

を...と 'のstd ::左/ STDを使用する::出力を左または右に揃えます。 –

関連する問題