change position

I want to print this triangle in the middle of console window.
I used gotoxy function but it only changes the position of first line of output.
Any help will be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
  #include <iostream>
using namespace std;
int main()
{
    int i,j,rows;
    cout<<"Enter the number of rows: ";
    cin>>rows;
    for(i=1;i<=rows;++i)
    {
        for(j=1;j<=i;++j)
        {
           cout<<"* ";
        }
        cout<<"\n";
    }
    return 0;
}
@rubait

Here is a way to use gotoXY() function to do what you were after.

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
// Triangle.cpp : main project file.

#include <iostream>
#include <Windows.h>


void gotoXY(int x, int y);

using namespace std;

int main()
{
    int i,j=1,rows;
    cout<<"Enter the number of rows: ";
    cin>>rows;
    for(i=1;i<=rows;++i)
    {
        gotoXY(40-i,5+j);// Starts in the center of screen, and moves left as 'i' increases
		for(j=1;j<=i;++j)
        {
           
			cout<<"* ";
        }
        cout<<"\n";
    }
    return 0;
}

void gotoXY(int x, int y) 
{ 
	HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD CursorPosition;
	CursorPosition.X = x; 
	CursorPosition.Y = y; 
	SetConsoleCursorPosition(console,CursorPosition); 
}
Thank You Sir
Topic archived. No new replies allowed.