Strange char duplication

Hello!

I am currently having a strange issue where the '@' char is duplicated when being sent to cout. Heres the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <windows.h>
#include <string>

int main();
{
    char wall = 177;

    char Map[14][40] = { "#######################################",
		         "#.....................................#",
			 "#......#######........................#",
		         "#......#######........................#",
			 "#......#######........................#",
			 "#......###.###........................#",
			 "#.....................................#",
			 "#.......................###.###.......#",
			 "#.......................#######.......#",
		         "#.......................#######.......#",
			 "#.......................#######.......#",
			 "#.@...................................#",
			 "#######################################" };

    for (int i = 0; i < 14; ++i)
    {
        for (int j = 0; j < 40; ++j)
        {
            if (Map[i][j] == '@')
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
                cout << Map[i][j];
            }
            if (Map[i][j] == '#')
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
                cout << wall;
            }
            else
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8);
                cout << Map[i][j];
            }
        }

        cout << endl;
    }

    return 0;
}


It for some reason duplicates the '@' char and outputs this:

▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒
▒.....................................▒
▒......▒▒▒▒▒▒▒........................▒
▒......▒▒▒▒▒▒▒........................▒
▒......▒▒▒▒▒▒▒........................▒
▒......▒▒▒.▒▒▒........................▒
▒.....................................▒
▒.......................▒▒▒.▒▒▒.......▒
▒.......................▒▒▒▒▒▒▒.......▒
▒.......................▒▒▒▒▒▒▒.......▒
▒.......................▒▒▒▒▒▒▒.......▒
▒.@@...................................▒
▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒



Any ideas?

(P.S if it helps the @ char on the left side is light cyan and the other is dark gray along with all the . chars, and the chars are light red)
Replace if on line 32 with else if
Now your code checks if cgaracter is @, prints it and if/else on lines 32-41 are executed too.
Last edited on
The problem is that this:
1
2
3
4
5
            if (Map[i][j] == '@')
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
                cout << Map[i][j];
            }


and this:
1
2
3
4
5
6
7
8
9
10
            if (Map[i][j] == '#')
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
                cout << wall;
            }
            else
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8);
                cout << Map[i][j];
            }


are two separate statements. The "else" will trigger if the second "if" is false, but it doesn't care about the first one.

Try making the second one an "else if" statement:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
            if (Map[i][j] == '@')
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 11);
                cout << Map[i][j];
            }
            else if (Map[i][j] == '#')
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 12);
                cout << wall;
            }
            else
            {
                SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 8);
                cout << Map[i][j];
            }
Last edited on
Wow thanks! It worked! It's amazing how such little things can have such a large difference! Also thanks Xish for making that clear to me it's one of those things I just didn't really understand
Topic archived. No new replies allowed.