The code I'm using:
cout<<setw(70)<<left<<"\n\n1. Monthly salary";
cout<<": RM";
cin>> monthsal;
cout<<"2. Any KWSP deduction?\n";
cout<<setw(70)<<left<<" Type 'Y' for Yes and 'N' for No";
cout<<": ";
cin>>kwsp;
Desired output:
1. Monthly salary : RM3200
2. Any KWSP deduction?
Type 'Y' for Yes and 'N' for No : Y
(Aligned on the colons.)
My code's output:
1. Monthly salary : RM3200
2. Any KWSP deduction?
Type 'Y' for Yes and 'N' for No : Y
How do I get my output to align correctly?
You can try this code
cout<<setw(70)<<left<<"\n1. Monthly salary";
cout<<": RM";
cin>> monthsal;
cout<<"2. Any KWSP deduction?";
cout<<setw(70)<<left<<"\n Type 'Y' for Yes and 'N' for No";
cout<<": ";
cin>>kwsp;
Consider: cout << setw(5) << left << "\n\nOK;" << "Hi";
cout << setw(5) << left << "\n\nOK;" << "Hi";
, "\n\nOK!"
actually already consumed all 5 spaces that was set from setw(5)
, because despite '\n'
are not printed like a single width character, they still consumes one space. So this will actually print like:
OK!Hi <-- notice no spaces between `OK!` and `Hi`
Now back to your case, if you have actually counted the character in the same line as "1. Monthly salary"
, you will notice the first block is actually less than 70 character for the same reason.
To fix it, you will want to print the "\n\n"
before your setw
:
cout << "\n\n";
cout << setw(70) << left << "1. Monthly salary";
⋮
For similar reason, '\t'
could also mess up your formatting, because the tab symbol is also considered a single character.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.