1

I am attempting format the output of a file using setw with no success. I understand that my output is shifting based on the character length of the variable before it, however I do not know how to change my code so that each row will print independent of the row before it. (Please see image link)

Due to the word "Swimming" being longer than all other word types, this shifts the information for those columns, making the report look terrible. Additionally, it seems like the comma in the "Amount" Column is shifting the output as well.

fout    << s.getID() 
        << right << setw(11) << addCommas(s.getAmount()) << "\t"
        << left << setw(14) << s.getType() << "\t"
        << left << setw(14) << s.getLength() << "\t"
        << left << setw(10) << s.getDate() << "           "
        << left << setw(15) << s.getFname() << "\t"
        << s.getLname() << endl;
lineCount++;

Current Program Output

(Currently messing with those setw values so please disregard that they don't line up with the header at the moment)

EDIT: Changed my font in VS to Helvetica because Consolas hurts my eyes, this also affected the output of my .dat, so essentially this was a non-issue. Yikes.

WRKS
  • 13
  • 3
  • Either set the widths for the worst possible case or compute the worst case for the data set with an extra pass through the data set. – user4581301 Sep 30 '19 at 16:53
  • 1
    Putting the `\t` in there won't make it any easier, since what a tab does in the output will format by rules external to the application. – Eljay Sep 30 '19 at 17:04

1 Answers1

0

If you add std::internal to your std::cout statement, it'll pump the remaining spaces out, and you can do a new setw after that. This might work better than tabs for you, especially if you're not using a monospaced font. See an example in this answer. Make sure your setw is large enough to accommodate your largest words... and don't do using namespace std, it's poor practice. Shame on you! ;)

Nick Reed
  • 4,989
  • 4
  • 17
  • 37
  • 1
    Thank you very much Nick! I dug into your question when you mentioned the font and that was the exact issue. You can't see it now, but my hands are definitely in my palms... – WRKS Sep 30 '19 at 18:32
  • You're very welcome! Please remember to accept the answer if it addresses your question. Good luck, and welcome to StackOverflow! – Nick Reed Sep 30 '19 at 19:27
  • 1
    Sorry, extremely new to the site. I've accepted the answer. Appreciate the help! – WRKS Oct 01 '19 at 15:41