Can any one help me put my pointer by gotoxy function inside the matrix that,s like as below:

void gotoxy( short x, short y ) // function to handle position of cursor on the screen
{

HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
COORD position = { x, y };

SetConsoleCursorPosition( hStdout, position );
}
int main()
{

    int row;
    int col;
    int *array[row][col];
    cout<<"Enter value of row and column for square matrix:"<<endl;
    cin>>row;
    cout<<endl;
    cout<<endl;
    col=row;
    int count=0;
    for (int i = 1; i <= row; i++)
    { 
      cout<<"|";
      for (int j = 1; j <= row; j++)
          {
           cout<<"---";
           for(int k=0;k<=count;k++)
           {
              cout<<"|";
              }
              }
              cout<<endl;     
              }
      system("pause");
      return 0;
     }

Dani AI

Generated

For : the reason your cursor jumps (or your code prints junk) is twofold — storage and coordinate mapping. As hinted, you must allocate the matrix after you know the sizes (don’t declare arrays using uninitialized variables), and you must convert matrix indices into console X/Y coordinates that are inside the console buffer before calling the cursor move. ’s request for an example is spot on — below is a compact, safe pattern you can adapt.

Use std::vector (or dynamic allocation) for the matrix, draw the ASCII grid once, then compute the screen position for each cell and call a bounds-checked cursor setter. The sample below shows a small helper that checks the console buffer with GetConsoleScreenBufferInfo before calling SetConsoleCursorPosition:

#include <windows.h>
#include <iostream>
#include <vector>
#include <iomanip>

bool setCursor(HANDLE hOut, int x, int y) {
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    if (!GetConsoleScreenBufferInfo(hOut, &csbi)) return false;
    if (x < 0 || x >= csbi.dwSize.X || y < 0 || y >= csbi.dwSize.Y) return false;
    COORD pos = { (SHORT)x, (SHORT)y };
    return SetConsoleCursorPosition(hOut, pos) != 0;
}

/* inside main(): allocate after reading rows/cols, draw separators,
   then place values at computed positions (see discussion below). */

Mapping notes and quick rules:

  • Decide a fixed cell width (cellW) and cell height (cellH) that match your ASCII separators (e.g., three dashes = cellW=3). Each cell’s left edge is at originX + col*(cellW+1) where +1 is the separator column. Center a value with +cellW/2.
  • Use zero-based indices (r from 0..rows-1, c from 0..cols-1) for the mapping.
  • Always check setCursor return; call GetLastError() if it fails — common causes are coordinates outside csbi.dwSize or an invalid handle.
  • Prefer std::vector<std::vector<int>> for safety; avoid VLAs or uninitialized stack arrays.

If you need a cross-platform approach, look at ncurses/pdcurses instead of Windows APIs.

Recommended Answers

All 2 Replies

Not a clue what you are asking. Examples may make it clear.

Let's start at the beginning.

1.) At line 14, you declare a two-dimensional array of int-pointers using uninitialized values of row and col, so who knows how big your matrix actually is.

2.) You then input a value for row, and assign col to be the same value, in lines 16 and 19.

3.) Finally you attempt to print out a bewildering set of junk characters, presumably looking something like:

|---|---|---|---|
|---|---|---|---|
...

... except you loop j over "row" instead of "col" (even though they're the same in this case, it's still silly); i can't tell why you loop k over [0,count] where count is also zero, is this so you can print more than one separator between successive vals in a row?; and you never reference your array of int-pointers at all. Of course that last point isn't an issue, since you also never fill your array with any values. And if you want it to store integers, it should be declared as int array[row][col]; (note, I removed the "*").

Also, note that Narue added [ CODE ] tags for you. Please do this yourself in the future. Use the "[ CODE ]" icon-button at the top of the editor. You have three options:
1) click the button first, then paste your code in between the start-tag and end-tag
2) paste your code first, then select it all and click the button
3) hand-type the tags yourself
whichever best suits your style.

Now after all that, what was the question?

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.