0

What I am trying to do is to move a character using arrow keys within a 1D or 2D array. I have initialized the array with "a" which means it starts at index #0 but when I swap it doesn't give the intended output instead it gives me something like that:

a a a a a a a a a

it should be like this

                a

Here is my code

#include<iostream>
#include<Windows.h>
#include<conio.h>
#include<fstream>
#define RIGHT 77
#define LEFT 75
#define UP 72
#define DOWN 80
#define ESC 27



using namespace std;
void gotoxy(int, int);
void swap(char*,char*);

char arr[1024] = { "a"};
char *line_ptr = (char*)arr[0];
char *line_ptr2d =(char*)arr[0];


void main()
{
    for (int i = 1; i < 1024; i++)
    {
        arr[i] = ' ';
    }

    int x = 1;
    int y = 1;
    int ch;

    do
    {   
        //system("cls");
        int i = 0;
        gotoxy(x, y);
        ch = _getch();

        if (ch == 224)
        {
            ch = _getch();
            switch (ch)
            {
            case LEFT:
                x -= 1;
                line_ptr--;
                swap(&arr[i], &arr[i + 1]);
                //arr[i] = ' ';
                cout << arr[i];
                break;
            case RIGHT:
                x += 1;
                swap(&arr[i], &arr[i + 1]);
                if (arr[i-1] != NULL)
                {
                    arr[i - 1] = ' ';
                }
                cout << arr[i+1];
                break;
            case UP:
                y--;
                break;
            case DOWN:
                y++;
                break;
            }
        }
        i++;
    } while (1);
} 

void gotoxy(int x, int y)
{
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    _COORD pos;
    pos.X = x;
    pos.Y = y;

    SetConsoleCursorPosition(hConsole, pos);
} 

void swap(char *x, char *y)
{
    char temp = *x;
    *x = *y;
    *y = temp;
}

2 Answers 2

1

I'll answer your question strictly and then I'll point you to some other issues in your code.

The You current problem is that you print incorrectly the data. For example when you "go" right you print the character 'a' at the current position and the you perform your gotoxy(x,y). While you need to put ' ' at you current position and then use gotoxy(x,y) with the updated values and put 'a'.

-

Some other issues:

1.

char *line_ptr = (char*)arr[0];
char *line_ptr2d =(char*)arr[0];

What? Why? Did you mean:

char *line_ptr = arr;
char *line_ptr2d = arr;

2.

You don't check for being inside your array limits.

3.

You always advance your index i no matter if you move left or right. (In such case you should probably use you existing x index instead).

4.

char arr[1024] = { "a"}; // You probably meant char arr[1024] = {'a'};
Sign up to request clarification or add additional context in comments.

Comments

0

After swappning two characters in the array, you just display the new one

swap(&arr[i], &arr[i + 1]);
cout << arr[i];

You also have to write a space over the i + 1 position to make it go away.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.