0

When I run the following program it displays the preceding zeros correctly:

#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;    
}

And I get the following output:

Enter ONE line containing at least six (6) integers. 123 67 122 90 7674 3 000012300000670000122000009000076740000003

But when I add another line of output that also requires me to use setw, all of a sudden it changes the preceding zeros into trailing zeros.

Program:

#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;
}

Gives me this output:

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

How do i stop it from changing my preceding zeros to changing zeros?

WhozCraig
  • 65,258
  • 11
  • 75
  • 141
user3281580
  • 53
  • 1
  • 2
  • 5
  • 1
    You may find [this question about sticky iofmt flags](http://stackoverflow.com/questions/1532640/which-iomanip-manipulators-are-sticky/1533222#1533222) an interesting read. – WhozCraig Feb 07 '14 at 06:07
  • @WhozCraig Thanks but this doesn't exactly explain how to fix my problem. – user3281580 Feb 07 '14 at 06:33
  • 1
    Do you *not* think `left`, the only manipulator different in your line in question in the second snippet but not in the first, has something to do with that? – WhozCraig Feb 07 '14 at 08:08

0 Answers0