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>
usingnamespace 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;
}
// Triangle.cpp : main project file.
#include <iostream>
#include <Windows.h>
void gotoXY(int x, int y);
usingnamespace 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);
}