I am learning C through K & R and I have attempted to write program for Ex 1-17, which should print lines having character more than 80. Written code works fine for lines having character lesser than 80 but for lines having greater than 80 characters my compiler hangs run time. This is how I give an input- I enter random 80+ characters and finally press an enter and my compiler hangs I have to force terminate it. I'm using Turbo c++ v4.5 on windows XP. My question is why my compiler hangs after pressing an enter? Please help me out with this code.
#include<stdio.h>
/* Program to print lines having length more than 80 chars */
#define MAX 80
#define MAXSIZE 1000
int getline( char a[], int b );
void copy ( char to[], char from[] );
main()
{
int len1, len2;
char line [ MAXSIZE ];
char longest [ MAXSIZE ];
len1 = len2 = 0;
while( ( len1 = getline( line , MAXSIZE ) ) > 0 ) /* Check if there is a line */
{
if( len1 > MAX && len1 > len2 )
{
len2 = len1;
copy( longest, line );
}
}
if( len2 > MAX )
printf("%s", longest);
return 0;
}
int getline( char a[], int b )
{
int i, c;
for( i = 0; i < b - 1 && ( c = getchar() ) != EOF && c != '\n'; ++i )
a [ i ] = c;
if( c == '\n' )
{ /* In this section for loop is must the only way to insert \n and \0 in an array remember this method */
a [ i ] = '\n' ;
++i;
}
a [ i ] = '\0';
return i;
}
void copy( char to[], char from[] ) /* For Copying a longest line */
{
int i = 0;
while( ( to[ i ] = from [ i ] ) != '\0' );
++i;
}