setw a table?
Nov 16, 2011 at 5:01pm UTC
I having problem with how to make a proper column and hard to understand setw..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
// setw example
#include <iostream>
#include <iomanip>
#include <conio.h>
using namespace std;
int main () {
cout << setfill(' ' );
cout << "DATE" ;
cout << setw(28) << "AMOUNT SPEND" ;
cout << setw(27) << "AMOUNT LEFT" ;
cout << "\n17/11/2011" ;
cout << setw(17) << "-RM1.00" ;
cout << setw(29) << "RM900.00" ;
getch();
return 0;
}
It seem ok when I execute it but however when the user input (amount spend) is more digit and also the amount left may change, then everything didn't display in proper column..
Nov 16, 2011 at 5:58pm UTC
You'll need to use variables in your setw() calls. Determine the string length of your numeric output, and use that as a basis for your calculations for setw().
Nov 17, 2011 at 1:44am UTC
the string length is defined by user. Do you mean set the max possible length?
Nov 17, 2011 at 4:59am UTC
Maybe I didn't understand your question: Here's a mild modification of your program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
// setw example
#include <iostream>
#include <iomanip>
using namespace std;
int main () {
cout << "00000000001111111112222222222333333333344444444444555555555" << endl;
cout << "12345678901234567890123456789012345678901234567890123456789" << endl;
cout << setfill(' ' );
cout << "DATE" ;
cout << setw(28) << "AMOUNT SPEND" ;
cout << setw(27) << "AMOUNT LEFT" ;
cout << "\n17/11/2011" ;
cout << setw(17) << "-RM1.00" ;
cout << setw(29) << "RM900.00" ;
return 0;
}
All four output lines are 59 characters. Is this not what you wanted?
Nov 17, 2011 at 12:11pm UTC
nevermind..
Topic archived. No new replies allowed.