Displaying outputs

Hello,

I have a txt file as an input which have 300 lines information and after some processes like sorting, calculating, I need to display these lines. I need to write function list, but because of too much information will be printed one time I want to control output. Like; first it will display 50 outputs and after N key it will display next 30 output till end of 300 lines. Any suggestion?
I made one for loop which displays first 30 lines and asked user to press N key like below but it looks like very long way to make 10 times different loop :S isn't it?

* I read system CLS command is not realy good to use but what do you suggest instead?

Thanx in advance!

void list(struct employeeInfo x[], int n)
{
char next;

for(int i=0; i<30; i++)
{
system("CLS")
printf("%s %s %s %s %d"
,x[i].depCode,x[i].empNr,x[i].firstName,x[i].lastName,x[i].age);
}
printf("Pres N key for next page..");
scanf("%c",&next);
if (next!=='n')
printf("Oupss! Wrong key!!");
else
for(int j=30; j<60; j++)
{
system("CLS")
printf("%s %s %s %s %d"
,x[j].depCode,x[j].empNr,x[j].firstName,x[j].lastName,x[j].age);
}
printf("Pres N key for next page..");
scanf("%c",&next);
if (next!=='n')
printf("Oupss! Wrong key!!");
else
for .....

}
Last edited on
Any suggestion? short way to solve problem above?
I would say write your for loop for x amount of loops. under put something like
1
2
cout<<"Push enter to continue"<<endl;
cin.ignore(256,'/n');


that should cause make them push enter to proceed
You will need to use the Win32 Console Functions
http://www.google.com/search?btnI=1&q=msdn+console+functions

The typical way to do it is to list a screenful at a time. Remember also to provide a way out. The following code snippit is an example of how it can be done.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <stdio.h>
#include <stdlib.h>

#include <windows.h>

struct employeeInfo
  {
  char* depCode;
  char* empNr;
  char* firstName;
  char* lastName;
  int   age;
  };

/* ---------------------------------------------------------------------------
 * This function is basically the Windows way to do isatty().
 */
int isconsole( HANDLE handle )
  {
  DWORD foo;
  return GetConsoleMode( handle, &foo ) != 0;
  }

/* ---------------------------------------------------------------------------
 * Returns the number of lines and columns currently VISIBLE.
 */
void windowsize(
  int* lines,
  int* columns
  ) {
  HANDLE hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  if (isconsole( hStdOut ))
    {
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    GetConsoleScreenBufferInfo( hStdOut, &csbi );
    *lines   = csbi.srWindow.Bottom - csbi.srWindow.Top  +1;
    *columns = csbi.srWindow.Right  - csbi.srWindow.Left +1;
    }
  else *lines = *columns = 0;
  }

/* ---------------------------------------------------------------------------
 * Return the number of lines currently VISIBLE.
 */
int linecount()
  {
  int rows, cols;
  windowsize( &rows, &cols );
  return rows;
  }

/* ---------------------------------------------------------------------------
 * Clear the entire window and home the cursor.
 */
void clear()
  {
  HANDLE                     hStdOut;
  CONSOLE_SCREEN_BUFFER_INFO csbi;
  DWORD                      count;
  DWORD                      cellCount;
  COORD                      homeCoords = { 0, 0 };

  hStdOut = GetStdHandle( STD_OUTPUT_HANDLE );
  if (hStdOut == INVALID_HANDLE_VALUE) return;

  /* Get the number of cells in the current buffer */
  if (!GetConsoleScreenBufferInfo( hStdOut, &csbi )) return;
  cellCount = csbi.dwSize.X *csbi.dwSize.Y;

  /* Fill the entire buffer with spaces */
  if (!FillConsoleOutputCharacter(
    hStdOut,
    (TCHAR) ' ',
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Fill the entire buffer with the current colors and attributes */
  if (!FillConsoleOutputAttribute(
    hStdOut,
    csbi.wAttributes,
    cellCount,
    homeCoords,
    &count
    )) return;

  /* Move the cursor home */
  SetConsoleCursorPosition( hStdOut, homeCoords );
  }

/* ---------------------------------------------------------------------------
 * Wait for the user to "Press a key to continue...".
 * Returns the Virtual Key Code of the key that was pressed.
 */
int pressanykey( const char* prompt )
  {
  DWORD        mode;
  HANDLE       hStdIn;
  INPUT_RECORD inrec;
  DWORD        count;
  char         default_prompt[] = "Press a key to continue...";

  /* Set the console mode to no-echo, raw input, */
  /* and no window or mouse events.              */
  hStdIn = GetStdHandle( STD_INPUT_HANDLE );
  if (!GetConsoleMode( hStdIn, &mode )
  ||  !SetConsoleMode( hStdIn, 0 ))
    return;

  if (!prompt) prompt = default_prompt;

  /* Instruct the user */
  WriteConsole(
    GetStdHandle( STD_OUTPUT_HANDLE ),
    prompt,
    lstrlen( prompt ),
    &count,
    NULL
    );

  /* Clear all input and wait for a key press */
  FlushConsoleInputBuffer( hStdIn );
  WaitForSingleObject( hStdIn, INFINITE );

  /* Wait for and get a single key RELEASE */
  do ReadConsoleInput( hStdIn, &inrec, 1, &count );
  while ((inrec.EventType != KEY_EVENT) && inrec.Event.KeyEvent.bKeyDown);

  /* Restore the original console mode */
  SetConsoleMode( hStdIn, mode );

  return inrec.Event.KeyEvent.wVirtualKeyCode;
  }

/* ------------------------------------------------------------------------ */
void list_employees(
  struct employeeInfo employee_info[],
  int    employee_count
  ) {
  int lines = linecount() - 1;
  int n;

  for (n = 0; n < employee_count; n++)
    {
    printf(
      "%-6s %-12s %s %s %d\n",
      employee_info[ n ].depCode,
      employee_info[ n ].empNr,
      employee_info[ n ].firstName,
      employee_info[ n ].lastName,
      employee_info[ n ].age
      );
    if ((n != 0) && ((n % lines) == 0))
      {
      int key = pressanykey( "Press ENTER to continue, ESC to quit" );
      printf( "\n" );
      if (key == VK_ESCAPE) return;
      }
    }
  }

/* ------------------------------------------------------------------------ */
int main()
  {
  #define NUM_EMPLOYEES 300
  struct employeeInfo employees[ NUM_EMPLOYEES ];
  int n;

  for (n = 0; n < NUM_EMPLOYEES; n++)
    {
    employees[ n ].depCode   = "ITCS";
    employees[ n ].empNr     = "000-00-0000";
    employees[ n ].firstName = "John";
    employees[ n ].lastName  = "Doe";
    employees[ n ].age       = n;
    }

  list_employees( employees, NUM_EMPLOYEES );

  return 0;
  }


Some additional notes:

1) Don't give the user a hard time for pressing something other than 'N'. The user may actually want to stop seeing the listing.

2) Make your output pretty and readable.

3) Use meaningful identifier names.

4) You'll notice that each screenful is the same size. Sure, you can change this if you like... but why? (Meaning, you need to have a pretty good reason to do something the user won't expect.)

5) Use commentary and whitespace to make your code easy to read.

Whew. Hope this helps.

[edit] Oops! Fixed an error above. /me embarrassed
[edit 2] Yoink! It wasn't an error! Un-Fixed. /me really embarrassed
Last edited on
Topic archived. No new replies allowed.