我有以下生成乘法表的程序。输出达到两位数时出现格式问题。我如何理顺这些列?在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