C++ results in column?
Feb 25, 2014 at 4:04am UTC
how do i get my c++ results in a column as follows:
http://s193.photobucket.com/user/skhjr/media/result_zps5edfc0d6.png.html?filters[user]=45699426&filters[recent]=1&sort=1&o=0
i know i will need to use setfill('-') and setw(?), but are the names of the items like name, hours worked, pay rate, etc. all on the same line with setw and are the dashes with another cout statement using setfill?
any help would be appreciated!
i currently have my code as below, but it isnt right.
cout << "Name" << setw(17) << "Hours worked" << setw(12) << "Pay rate" << setw(10) << "Gross Pay" << setw(9) << "Social Security" << setw(15)<< "Income Tax" <<
setw(10)<< "Net Pay" << setw(7) << endl;
Last edited on Feb 25, 2014 at 4:09am UTC
Feb 25, 2014 at 5:44am UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <array>
#include <iomanip>
#include <iostream>
#include <string>
int main()
{
std::array<std::string, 7> fields = {"Name " , "Hours worked" , "Pay rate" ,
"Gross Pay" , "Social Security" , "Income Tax" , "Net Pay" };
std::cout << std::left;
for (const auto & f: fields)
std::cout << std::setw(f.length() + 2) << f;
std::cout << std::right << std::setfill('-' ) << std:: endl;
for (const auto & f: fields)
std::cout << std::setw(f.length() + 2) << " " ;
}
http://ideone.com/wjX2dt
Topic archived. No new replies allowed.