1

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;
 }
  • Run in a debugger and step through the code line by line to see where the infinite loop happens. While Turbo C++ is ancient, I recollect that the Turbo-line of IDEs had pretty good debuggers for its time. – Some programmer dude Mar 29 '15 at 08:53
  • 1
    Have you pressed Ctrl-Z to indicate the end of input? If no, then your code is waiting for you to enter later lines. – timrau Mar 29 '15 at 08:56
  • @JoachimPileborg I agree with you Turbo c++ is ancient...I was planning to use Sublime Text 2 for learning C...but every time I've failed in its build file...perhaps can you help me with it? – swapnil mandavkar Mar 29 '15 at 11:02

2 Answers2

6

You are getting infinite loop because in your function void copy( char to[], char from[] ) you have

  while( ( to[ i ] = from [ i ] ) != '\0' );          
    ++i;

You left a semicolon after the while loop. Remove that and that will stop the infinite loop.

Due to the semicolon, the ++i is not part of the while loop and thus, the value of i is never incremented. That's the problem with your code.

Also note that main() should be int main(void) or int main(int argc, char *argv[]) because that's the standard. On TurboC++ , you might not need to specify the return type of main() , but the c standard clearly states that main() must return a type. Refer What should main() return in C and C++?

Aslo note that as indicated by @timrau in his comment, you will keep reading input until EOF is encountered, and for that, you will have to press Ctrl + Z. Otherwise, it will just keep asking for input ( and you'll get output only if you enter a string with more than 80 characters ) .

Community
  • 1
  • 1
Arun A S
  • 6,421
  • 4
  • 29
  • 43
3

You have an infinite loop in the copy function:

while( ( to[ i ] = from [ i ] ) != '\0' );          
++i;

The ++i is not part of the loop as there is a semi-colon.

Avi Ginsburg
  • 10,323
  • 3
  • 29
  • 56