countdown and pattern

Write your question here.
how do i make a hollow rectangle and put a countdown in it?
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
  for (i=0;i<=ni;i++) 
	{
		for (j=0;j<=nj;j++)
		{
			printf("*");	
				
		}
	
		printf("\n");
	}
/*this part is rectangle which looks like this **********
                                               **********
                                               **********     */



void Countdown (short nZeit)  //this part is countdown
{ 
	

	for (nZeit=10; nZeit>0; nZeit--)
	{
		printf("%d\b\b", nZeit);
		fflush(stdin);
		Sleep(1000);
	}

	
}

/*i need to make a code like this:

******************************
***                        ***
***  Simulation start in   ***
***                        ***
***       10 seconds       ***
***                        ***
******************************

the 10 seconds is a countdown*/



Last edited on
You cannot achieve that with standard C++. So you need to use either a library like ncurses or the system api.
On windows you may have the header #include <conio.h> but it isn't part of the standard so may not be present, or if it is, the functions available may differ, or behave differently. You could try gotoxy() or write your own, see this thread and post from Duoas, http://www.cplusplus.com/forum/beginner/4234/#msg18563

If you are not using windows, or want a cross-platform solution, look into ncurses as suggested.
i read a couple of things about gotoxy(),i believe it is SetCursorPostion in my case but i don't really get it.btw, i use Microsoft visual studio
The post I linked to shows how to use SetConsoleCursorPosition in order to make your own version of gotoxy. That's just for convenience, it makes it simpler to use.

The idea is that the console is considered as a grid of rows and columns, before using printf or cout, first position the cursor where you'd like it. Really, the only way to understand better is to try it out, experiment a little.

First, a built-in version of gotoxy
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <conio.h>

int main()
{
    for (int i=0; i<7; i++)
        std::cout << "***********\n";

    gotoxy(3, 4);
    std::cout << " Hello ";

    return 0;
}
***********
***********
***********
** Hello **
***********
***********
***********


And a version built using windows functions.
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
#include <iostream>
#include <windows.h>

void gotoxy( int column, int line )
{
  COORD coord;
  coord.X = column;
  coord.Y = line;
  SetConsoleCursorPosition(
    GetStdHandle( STD_OUTPUT_HANDLE ),
    coord
    );
}

int main()
{
    for (int i=0; i<7; i++)
        std::cout << "***********\n";

    gotoxy(2, 3);
    std::cout << " Hello ";
    gotoxy(3, 8);

    return 0;
}
***********
***********
***********
** Hello **
***********
***********
***********


Note: I used two different compilers, but neither of them was Microsoft visual studio.
Topic archived. No new replies allowed.