当我运行以下程序时,它会正确显示前面的零:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//Code
int num1, num2, num3, num4, num5, num6;
cout << "Enter ONE line containing at least six (6) integers." << endl;
cin >>num1>>num2>>num3>>num4>>num5>>num6;
cout << setfill('0') << setw(7) << num1 << setfill('0') << setw(7) << num2 << setfill('0') << setw(7) << num3 << setfill('0') << setw(7) << num4 << setfill('0') << setw(7) << num5 << setfill('0') << setw(7) << num6 << endl;
return 0;
}
我得到以下输出:
输入包含至少六 (6) 个整数的一行。123 67 122 90 7674 3 000012300000670000122000009000076740000003
但是当我添加另一行也需要我使用 setw 的输出时,它突然将前面的零更改为尾随的零。
程序:
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
//Code
int num1, num2, num3, num4, num5, num6;
cout << "Enter ONE line containing at least six (6) integers." << endl;
cin >>num1>>num2>>num3>>num4>>num5>>num6;
cout << "1: " << num1 << endl;
cout << "2: " << num2 << endl;
cout << "3: " << num3 << endl;
cout << "4: " << num4 << endl;
cout << "5: " << num5 << endl;
cout << "6: " << num6 << endl;
cout << "---------------------" << endl;
cout << ">" << setw(6) << num1 << setw(8) << right << num2 << setw(5) << left << num3 << "<" << endl;
cout << ">" << setw(6) << left << num4 << setw(8) << right << num5 << setw(5) << left << num6 << "<" << endl;
cout << "---------------------" << endl;
cout << setfill('0') << setw(7) << left << num1 << setfill('0') << setw(7) << num2 << setfill('0') << setw(7) << num3 << setfill('0') << setw(7) << num4 << setfill('0') << setw(7) << num5 << setfill('0') << setw(7) << num6 << endl;
return 0;
}
给我这个输出:
Enter ONE line containing at least six (6) integers.
568 989 33 90 83 3
1: 568
2: 989
3: 33
4: 90
5: 83
6: 3
---------------------
>568 98933 <
>90 833 <
---------------------
568000098900003300000900000083000003000000
我如何阻止它将我前面的零更改为更改零?