i have this constructor to display a bitmap and it displays with 1's and 0's..i jus need 2 know how 2 make the 0's into *'s...i think its an if statement but i dont know exactly were

void bitmap::display() const
{
     for (int r = 0; r < numrows; r++)
     {
         for (int c = 0; c < numcols; c++)
            cout << grid[r][c] <<  ' ';
         cout << endl;

     }

}

Dani AI

Generated

Two things explain why the 0s kept showing up: the data type stored in the grid, and C++ operator precedence when combining the conditional operator with stream insertion.

If the grid holds character digits ('0' and '1') then testing the cell as a boolean will not work: the character '0' has a nonzero numeric value (ASCII 48), so it evaluates as true. In that case an explicit equality check against the character digit is required. If the grid stores numeric 0/1 (int, unsigned, or bool) an explicit numeric comparison or a boolean test is appropriate.

The other common pitfall is that the insertion operator and the conditional operator interact in surprising ways. A ternary expression placed directly between two << operations will not be parsed the way it looks; the stream insertion can bind first. That is why s suggestion produced no visible change. The fix is to make the intent explicit: either parenthesize the entire conditional so the stream receives its result, or use a simple if/else to choose the display character before inserting it. Both approaches were suggested by and are correct.

Quick diagnostics: inspect the grid declaration to determine its type; print an element as an integer to see whether it is 0/1 or an ASCII code; then use an explicit comparison and ensure the conditional result is what is passed to cout. This eliminates the ambiguity and makes the output predictable.

Recommended Answers

All 4 Replies

cout << grid[r][c]?'1':'*' << ' ';

when i replaced my cout statement with yours the 0's were still ther?

is grid an array chars?

cout << (grid[r][c] == '1') ? '1'  : '*' << ' ';

or

if(grid[r][c] == '0') cout << '*';
else cout << '1';

thanks man

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.