Strange output format error

This is pretty straight forward...

My output code:

1
2
3
//test_set is a string containing everything after "failed: " to the first "-"
//expected is a boolean value.
out << std::setfill('-') << std::setw(80) << std::left << test_set << "expected: " << expected << '\n';


Produces:

Test #15 failed: {}}------------------------------------------------------------expected: 0
Test #18 failed: {}}}}----------------------------------------------------------expected: 0
Test #32 failed: {1,2,3}a-------------------------------------------------------expected: 0
Test #33 failed: {{1,2},{3,a},{5,6},{b,c}}}a------------------------------------expected: 0
Test #35 failed: {1,2,3} 	a-----------------------------------------------------expected: 0
Test #36 failed: {1,2,3}{-------------------------------------------------------expected: 0
Test run complete.


Hopefully the error is clear. I have no idea why it happens or how I can fix it. Can anyone help?
Last edited on
It looks like you have a tab character '\t' embedded in this string "{1,2,3} a". It's not likely to work well in combination with setw().
I do. It's intentional. Why would that not work well with setw() and how do I fix it?
Last edited on
I do. It's intentional.
May I ask what the intention might be?

Why would that not work well with setw()
Well, for one thing the number of character positions occupied by a tab character is not defined by the program, but is dependent on the display device. Another, its exact behaviour will depend on how many characters already appear on the line, before the tab.

and how do I fix it?
Ideally, find the code which puts the tab there and change it to use one or more spaces instead. If that's not possible, then you could modify the string test_set before outputting it
1
2
3
        size_t pos = test_set.find('\t');
        if (pos != test_set.npos)
            test_set[pos] = ' ';
May I ask what the intention might be?


This mini-program is meant to take input and produce output on whether the input can be considered a mathematical set as defined by:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    Set:
        "{" Sequence "}"

    Sequence:
        <Empty>
        List

    List:
        Element
        Element "," List

    Element:
        Alpha-numeral
        Set


What you see here is the output to my test set (taken from a text file) after intentionally creating a bug in the program (to test out my test harness). The bug you see in this case is caused when extraneous input is left over after a complete set has been read. In this case, extraneous input should not be accepted and should fail the test. However, extraneous white space is acceptable. In order to be thorough, I added test cases where extra input was placed immediately after the set in some cases and after a padding of white space in other cases.

TLDR; The tab character is intentional for testing purposes.
Last edited on
Ok - in effect the tab character was part of the program input - in that case you are left with having to handle it, as best you can.
Topic archived. No new replies allowed.