i have here this constructor used 2 display a bitmap

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

i have here this invert function which is meant to make the 0's into 1's and the * into " " but it is not working proply can ne1 help me

void bitmap::invert()
{
     for (int r = 0; r < numrows; r++)
     {
         for (int c = 0; c < numcols; c++)
         {
             if (grid[r][c] = 1)
             {
             grid[r][c] = 0;
             cout << "*";
             }
             else{
             grid[r][c] = 1;
            cout << " ";
             }
         }
     }
}

Dani AI

Generated

Short follow-up tied to what and already pointed out: if you corrected the operator and split display from inversion but stars still look “far apart,” check a few things that often cause that visual result.

First, confirm what values are actually stored in grid (integers 0/1 vs. character '0'/'1' vs. literal '*'/' '), and print the raw numeric values for a quick check:

for (int r = 0; r < numrows; ++r) {
  for (int c = 0; c < numcols; ++c)
    cerr << int(grid[r][c]) << ' ';
  cerr << '\n';
}

If values are numeric 0/1, a compact invert that keeps presentation separate is:

void bitmap::invert()
{
  for (int r = 0; r < numrows; ++r)
    for (int c = 0; c < numcols; ++c)
      grid[r][c] ^= 1;   // flips 0<->1 for integer cells
}

If the cells hold character digits, convert accordingly:

grid[r][c] = (grid[r][c] == '1') ? '0' : '1';

Common causes of the “wide” layout: printing from the invert routine without emitting a newline at the end of each row (console wrapping can make output look spaced), having numcols larger than expected (long lines wrap), or printing multi-character strings per cell. Best practice: have invert() only mutate data; call your single display() routine afterward so formatting is handled in one place. Run a tiny test (3x5) and the numeric debug print above to quickly spot whether cells are the expected 0/1 values or something else.

Recommended Answers

All 4 Replies

Its a simple mistake.
if (grid[r][c] = 1)

= is an assinment operator. == is the relational operator

The code is just fine

i did that but the invert still doesnt work it displays, but the *'s and ' 's are all really far apart

I dont have much code to deal with, besides the constructor displays a '*' if mat element is 1 & the invert function does the same, even though it changes the matrix. Upload more code

Why does invert() also display the result?
Shouldn't you just call display() when you're done?

Each function does ONE job properly.

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.