cout array one page at a time

i have an array of 20000 elements and i need to print out but it has to be done in
a clean matter filling one page and continue when the user wants
here is what i have so far it print out my array but it would just go for ever


1
2
3
4
5
6
void showarray(const int array[], int size)
{
	for (int count = 0; count < size; count++)
		cout << array[count] << "  ";
	cout << endl;
}
this what i ended up doing is not the most effecient but it kind of worsk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void showarray(const int array[], int size)
{
	int check = 0;
	int continu;
	for (int count = 0; count < size; count++)
	{
		cout << array[count] << "  ";
		cout << endl;
		++check;
		if (check == 150)
		{
			cout << "**************************PRESS Any number to continue***********************" << endl;
			cin >> continu;
			if (cin.fail())
			{
				cout << "What the.... " << endl;
				cin.clear();
				cin.ignore(2000, '\n');
			}
			check = 1;
			continue;
		}
	}
}
Rather than printing a fixed number of spaces between each element, a neater result can be achieved by specifying the field width.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <iomanip>


void showarray(const int array[], int size)
{

    for (int count = 0; count < size; count++)
    {
        std::cout << std::setw(10) << array[count];   // print with a width of 10 characters

        if ((count+1)%5 == 0)                         // start a new line after each five elements
            std::cout << '\n';
    }

}

Example output:
        41      8467      6334      6500      9169
      5724      1478      9358      6962      4464
      5705      8145      3281      6827      9961
       491      2995      1942      4827      5436
      2391      4604      3902       153       292
      2382      7421      8716      9718      9895
      5447      1726      4771      1538      1869
      9912      5667      6299      7035      9894
      8703      3811      1322       333      7673
      4664      5141      7711      8253      6868
      5547      7644      2662      2757        37
      2859      8723      9741      7529       778
      2316      3035      2190      1842       288
       106      9040      8942      9264      2648
      7446      3805      5890      6729      4370
      5350      5006      1101      4393      3548
      9629      2623      4084      9954      8756
      1840      4966      7376      3931      6308
      6944      2439      4626      1323      5537
      1538      6118      2082      2929      6541

See http://www.cplusplus.com/reference/iomanip/setw/
and http://www.cplusplus.com/reference/ios/ios_base/width/
Last edited on
I would like to do it like that:

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
void showarray(const int array[], int size)
{
	const int LINES_PER_PAGE = 20;
	const int ITEMS_PER_LINE = 10;

	int line = 0, col = 0;

	for (int item = 0; item < size; item++)
	{
		cout << array[item] << " ";
		col++;
		if (col >= ITEMS_PER_LINE)
		{
			col = 0;
			line++;
			cout << endl;
		}
		if (line > LINES_PER_PAGE)
		{
			line = 0;
			cout << "Hit any key to continue";
			getch();
		}
	} // for
} // showarray


You would need to include <conio.h>

filling one page and continue when the user wants


With so many items the user might have to press a key a few hundred times;
Topic archived. No new replies allowed.