我有以下生成乘法表的程序。輸出達到兩位數時出現格式問題。我如何理順這些列?在C++中格式化列
#include <iostream>
using namespace std ;
int main()
{
while (1 != 2)
{
int column, row, c, r, co, ro;
cout << endl ;
cout << "Enter the number of columns: " ;
cin >> column ;
cout << endl ;
cout << "Enter the number of rows: " ;
cin >> row ;
cout << endl ;
int temp[column] ;
c = 1 ;
r = 1 ;
for(ro = 1; ro < row ; ro ++){
for(co = 1; co < column ; co ++){
c = C++ ;
r = r ++ ;
temp [c]= co * ro;
cout << temp[c] << " ";
}
cout << endl ;
}
system("pause");
}
}
你的程序中有一個很大的錯誤會使它經常崩潰:它應該是int temp [column * row]'。另外,如果你不想重複使用表格,你可以在循環內執行'cout << co * ro'並移除'temp'數組。 – schnaader 2010-09-28 01:42:54
我問爲什麼有一個臨時的。循環後不使用它。爲什麼不只是做'cout << setw(3)<< co * ro;'? – JoshD 2010-09-28 01:45:49