Hi, this is a code for Simple Console based text editor from Complete Reference to C++ by Herbert Shildt:
#include "stdafx.h"
#include "conio.h"
#define MAX 5
#define LEN 20
char text[MAX][LEN];
int _tmain(int argc, _TCHAR* argv[])
{
register int t, i, j;
printf("Enter an empty line to quit.\n");
for(t=0; t<MAX; t++)
{
printf("%d: ", t);
gets(&text[t][0]);
if(!*text[t]) break; /* directly hiting Enter key appends '\0' i.e. null, hence &text[t] will be null; quit on blank line */
}
for(i=0; i<t; i++)
{
for(j=0; text[i][j]; j++)
putchar(text[i][j]);
putchar('\n');
}
getch();
return 0;
}
I insert a letter 'a' 20 times on first line:
[image:http://img40.imageshack.us/f/52134015.png/]
& then 'b' 20 times:
{image: http://img59.imageshack.us/i/29952735.png/}
but at first, it stores 'a' correctly:
{image: http://img141.imageshack.us/i/79038161.png/}
however for second iteration, it stores the 'b's incorrectly:
[image: http://img689.imageshack.us/i/80327231.png/]
this continues further, if I enter 'c' 20 times & so on.
The final output is also wrong:
[image: http://img3.imageshack.us/i/49207766.png/]
However the code works great for small strings:
[image: http://img822.imageshack.us/i/31994060.png/]
Why is this overlapping?
Please help.