if (decision == "B" || decision == "b"){
        for (k =0; k < rows; k++){
        for (m =0; m < cols; m++)
        switch (image[k][m] = image[k][m] + brightcount)
        {
        case '0' : cout <<" ";
                   break;
        case '1' : cout <<"-";
                   break;
        case '2' : cout <<"=";
                   break;
        case '3' : cout <<"O";
                   break;
        case '4' : cout <<"Z";
                   break;
        case '5' : cout <<"X";
                   break;
        case '6' : cout <<"B";
                   break;
        case '7' : cout <<"@";
                   break;
        case '8' : cout <<"W";
                   break;
        case '9' : cout <<"#";
                   break;
 }
        cout<< (image[k][m]);
        cout<< "\n";
        }
}
else if (decision == "D" || decision == "d"){
        for (k =0; k < rows; k++){
        for (m =0; m < cols; m++)
        switch (image[k][m] = image[k][m] + darkcount)
 {
        case '0' : cout <<" ";
                   break;
        case '1' : cout <<"-";
                   break;
        case '2' : cout <<"=";
                   break;
        case '3' : cout <<"O";
                   break;
        case '4' : cout <<"Z";
                   break;
        case '5' : cout <<"X";
                   break;
        case '6' : cout <<"B";
                   break;
        case '7' : cout <<"@";
                   break;
        case '8' : cout <<"W";
                   break;
        case '9' : cout <<"#";
               break;
        }
        cout<< (image[k][m]);
        cout<< "\n";
        }
}

I'm trying to add more if/else statements inside of these two things going on. If I get some help with the first half I should be able to figure out the second half of it. Anyway for the "B" or "b" statement, if any of the image[k][m] are 0 then I don't want it to do the switch statement because then it'll take 0s and make them -1s which don't have a corresponding case.
I had an if statement right after the for loop had begun like:

for (k =0; k < rows; k++){
        for (m =0; m < cols; m++)
if (image[k][m] > 0){
followed by the switch stuff
Then the switch got ended
Then the if statement just made was ended
Then I had an else{
a cout saying "Cannot be brightened any further"
and then closed the else
Finally closed the for loop.

Sorry I still don't have the code there's alot of lines in the program and I didn't want to leave it busted, I returned the code to its last working state.
Anyway I just keep getting flooded with compiler errors, I know it can be done my mind is just fried by this plaguing issue.

Recommended Answers

All 10 Replies

The switch statement itself has an expression unconditionally modifying image[k][m], so you will have to add an if statement before the switch:

if (image[k][m] != 0)
  switch (image[k][m] = ...) {
    ...
    }
else cout << " ";

You also need to watch your indentation. The

cout << (image[k][m]);
cout << "\n";

lines belong to the outer for loop, not the inner loop. Hence, it will only output once for each row, and m will always equal cols (which I am thinking is an invalid index).

I am also wondering what type the elements of your image array are. Are they ints or chars?

It's an array of chars, I'm going to try and use what you've said to get the program running as I want it to.

if (decision == "B" || decision == "b"){
        for (k =0; k < rows; k++){
        for (m =0; m < cols; m++)
        if (image[k][m] != 0)
        switch (image[k][m] = image[k][m] + brightcount){
        case '0' : cout <<" ";
                   break;
        case '1' : cout <<"-";
                   break;
        case '2' : cout <<"=";
                   break;
        case '3' : cout <<"O";
                   break;
        case '4' : cout <<"Z";
                   break;
        case '5' : cout <<"X";
                   break;
        case '6' : cout <<"B";
                   break;
        case '7' : cout <<"@";
                    break;
        case '8' : cout <<"W";
                   break;
        case '9' : cout <<"#";
                   break;
        }
        else cout << "Image cannot be brightened further." << endl;
        cout<< (image[k][m]);
        cout<< "\n";
        }
}

That lets me brighten beyond what I should be able to, I never get the else statement.

}
        else cout << "Image cannot be brightened further." << endl;
        }
        cout<< (image[k][m]);
        cout<< "\n";
}

Then I gave that a change to see what would happen, and it doesn't output the image right. Instead of this:
====WWW===WW======WWW=
====WWW===WW======WWW=
OOOOOOOOOOOOOOOOOOOOOO
----------------------
-----ZZZZZ------------
----ZZZZZZZ--XX-------
---ZBZOZOZOZXXX-------
----OOOOOOO--XX-------
-----OOOOO--------X--Z
------------------XXZO

I get this:
----@@@---@@------@@@-----@@@---@@------@@@-======================
OOOOO OOOOOOO ZZ OXO=O=O=OZZZ ==
===== ZZ ===== Z O ZZO=

I'm going to keep the two cout statements where they are.

You aren't thinking it through.

Do this:

if (decision == "B" || decision == "b"){
    for (k =0; k < rows; k++){
        for (m =0; m < cols; m++)
            if (image[k][m] != 0)
                switch (image[k][m] = image[k][m] + brightcount){
                    case '0' : cout <<" "; break;
                    ...
                }
            else cout << ' ';  // image can't be brightened further
        cout << "\n";  // done this row
    }
}

In pseudo code:

if decision is B or b:
  for each row:
    for each colum:
      if given pixel != 0:
        modify pixel and display something (other than the modified pixel value)
      else: display a blank
    put out a newline at the end of the row

I'm still confused about how you are storing the colors of your image. If they are characters, then how is '\0' the brightest possible value? It can't even be displayed...

Is this supposed to be some sort of ASCII art transformation?

Yes, it's just taking in numbers and turning them into ASCII and it's supposed to be just ASCII art, nothing more.

I made a discovery as to why I was never getting the else statement. Because it's a char array, the if statement needed to read like this:

if (decision == "B" || decision == "b"){
        for (k =0; k < rows; k++){
        for (m =0; m < cols; m++)
        if (image[k][m] !=  '0')
        switch (image[k][m] = image[k][m] + brightcount){
        case '0' : cout <<" ";
                   break;
        case '1' : cout <<"-";
                   break;
        case '2' : cout <<"=";
                   break;
        case '3' : cout <<"O";
                   break;
        case '4' : cout <<"Z";
                   break;
        case '5' : cout <<"X";
                   break;
        case '6' : cout <<"B";
                   break;
        case '7' : cout <<"@";
                   break;
        case '8' : cout <<"W";
                   break;
        case '9' : cout <<"#";
                   break;
        }
        else cout <<"Image cannot be brightened further."endl;
        cout << "\n";
        }


}

I needed it to read '0' not just 0. So now I get the else statement, problem is whether I have else put out a statement or if I let the else put out just " ", it throws off the image all together. I tried doing an if statement that let it run through a for loop, checking for any zeros and then saying "Cant brighten further." if it fuond any, or else running through through the for loop again and doing the switch. But I got a ton of compiler errors, so I don't believe it's possible to call upon the for loop like that twice as I tried to do.

First things first -- put braces around everything that needs them -- around statements contained in if s, else s, switch es, for s, etc. And indent them properly. Then we can see what's going on.

while(cin){
if (decision == "B" || decision == "b"){
        for (k =0; k < rows; k++){
        for (m =0; m < cols; m++)
                if (image[k][m] !=  '0'){
                        switch (image[k][m] = image[k][m] + brightcount){
                        case '0' : cout <<" ";
                        break;
                        case '1' : cout <<"-";
                        break;
                        case '2' : cout <<"=";
                        break;
                        case '3' : cout <<"O";
                        break;
                        case '4' : cout <<"Z";
                        break;
                        case '5' : cout <<"X";
                        break;
                        case '6' : cout <<"B";
                        break;
                        case '7' : cout <<"@";
                        break;
                        case '8' : cout <<"W";
                        break;
                        case '9' : cout <<"#";
                        break;
                        }
                }
                else{
                cout <<"Image cannot be brightened further."endl;
                cout << "\n";
                    }
        }


}

I made it a little easier to see what's going on and put braces around everything, by the way when I put braces around everything as you asked the program doesn't compile anymore. And I'll state what's going on again and what I'm trying to make happen, it's reading in a character array of rows and columns and then depending on what comes in, it will be switched to something to make it look like ASCII art. When the user brightens, all the characters in the array have one subtracted from them, hence why at zero the image has been brightened so much there is nothing in the ASCII art image because of how much it's been brightened. Think of case '0' as the color white if you will. The user is allowed to brighten an image up until there is a zero now in the character array, then they are to be told that the image cannot be brightened further because then there would be image loss. So if all the characters in the array aren't 0, then it can do the switch and output the new image to the user, if any of them are 0, it should say "Sorry can't brighten further" and go back to the main menu of the program, where they can choose to darken, save the image, etc.

Your code was fine without the extra { and }. Your indenting could have used a little help though. Take a look at my example in post #5.

I think you are trying to do too many things at once.

If I understand what you want correctly
You have an image stored as an array of array of char, where each char/pixel has a value from '0' (white) to '9' (black).

You want to "brighten" the image, but only if there are no '0's in the image.

You also want to display the image using specific characters (in other words, '0' displays as ' '; '1' displays as '-'; etc.)

Thoughts
Brightening doesn't make much sense for ASCII art.

Brightening is a lossy function, where shades move towards white. Saying that an image cannot be further brightened is rather non-sequitor, unless the image is already pure white. Hence: 0-->0, 1-->0, 2-->1, 3-->2, etc.

Separate the function that modifies the image and the function that displays the image.

// test here to see if the image can be brightened or not
bool can_brighten_image( char *image, int rows, int cols ) {
  // in this example, I'm testing whether or not the image is pure white already
  for (int i = 0; i < (rows *cols); i++)
    if (image[ i ] != '0') return true;  // there's still non-white pels, so YES
  return false;  // all pels are white, so NO more brightening possible
  }

// Brighten the image, where 0-->0, 1-->0, 2-->1, etc.
void brighten_image( char *image, int rows, int cols ) {
  for (int i = 0; i < (rows *cols); i++)
    if (image[ i ] != '0') (image[ i ])--;
  }

// Display the image using our select mapping
void display_image( char *image, int rows, int cols ) {
  char mapping[] = " -=0ZXB@W#";
  for (int row = 0; row < rows; row++) {
    for (int col = 0; col < cols; col++, image++)
      cout << mapping[ *image -'0' ];
    cout << '\n';
    }
  }

// I've no clue where your main loop is, so I put it in main()...
int main() {
  ...

  if (decision == "B" || decision == "b") {
    if (can_brighten_image( image, rows, cols )) {
      brighten_image( image, rows, cols );
      display_image( image, rows, cols );
      }
    else cout << "The image cannot be brightened any further.\n";
    cout << endl;
    }

  ...
  return EXIT_SUCCESS;
  }

I still presume that decision is a std::string for you to be testing it like you do. You should be getting input with getline( cin, decision ); Hope this helps.

Oh yeah, (sorry for the double post)

If you still want to fail if any pixel is white

// test here to see if the image can be brightened or not
bool can_brighten_image( char *image, int rows, int cols ) {
  for (int i = 0; i < (rows *cols); i++)
    if (image[ i ] == '0') return false; // there's a white pel, so NO
  return true; // no pels are white, so YES more brightening possible
  }
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.