Search code examples
cconsole-applicationgetchar

This C program doesn't work


I copied the following C code from K&R. The code is supposed to print a line if it is currently the longest line typed by the user. This is the code:

 #include <stdio.h>

#define MAXLINE 1000
int max;
char line[MAXLINE];
char longest[MAXLINE];

int getline();
void copy();


int main(){

    int len=0;
    extern int max;
    extern char longest[];

    max = 0;
    while((len = getline()) > 0)
        if (len > max){

            max = len;
            copy();
        }

    if (max > 0){

        printf("%s", longest);
    }
    return 0;
 }


    int getline(){

    int c;
    int i;
    extern char line[];

    for(i = 0; i < MAXLINE-1
        &&(c=getchar())!=EOF&&c!='\n';++i)
            line[i] = c;

    if(c=='\n'){
        line[i]=c;
        ++i;
    }

    line[i] = '\0';
    return i;



}




      void copy(){

      int i;
      extern char line[];
      extern char longest[];

       i=0;

       while((longest[i]=line[i])!='\0')
        ++i;
      }

The problem is that it doesn't appear to work. Running the code, I can type lines into the console but it doesn't print the longest line. I would appreciate any help.

EXTRA INFO: I7m using Win7 with Open Watcom compiler. The console doesn't fold, it lets me input characters. Also, I'm not sure that I am stuck in a loop because if I change my while-loop in main() to

while((len = getline()) > 0)
        if (len > max){
            printf("IT WORKS");
            max = len;
            copy();
        }

with a print command if the length of the line is longer than the current max-length, then "IT WORKS" is printed on screen. So it is definitely counting line lengths.


Solution

  • The getline returns 1 higher than expected, so if the user just hits enter, it doesnt exit the loop. Also, add getch() before return 0 in your main function, the console exits/folds after printing the longest line. Many books have the same example so if you want the console to remain open, put getch so the user should strike a key before the program exits. Here, try these codes:

    #include <stdio.h>
    
    #define MAXLINE 1000
    int max;
    char line[4];
    char longest[MAXLINE];
    
    int getline();
    void copy();
    
    
    int main()
    {
        int len=0;
        extern int max;
        extern char longest[];
    
        max = 0;
                /* put -1 to getline so it will return the right length... */ 
        while(((len = getline()) - 1) > 0)
        {
            printf("%d \n",len);
            if (len > max)
            {
    
                max = len;
                copy();
            }
        }
    
        if (max > 0)
        {
    
            printf("%s", longest);
        }
    
                /* Put getch() or getchar() so console will wait for a key to be pressed before exiting */
        getch();
        return 0;
     }
    
    
    int getline()
    {
    
        int c;
        int i;
        extern char line[];
    
        for(i = 0; i < MAXLINE-1 &&(c=getchar())!=EOF&&c!='\n';++i)
        {
            line[i] = c;
        }
    
        if(c=='\n')
        {
            line[i]=c;
            ++i;
        }
    
        line[i] = '\0';
        return i;
    }
    
    
    
    
          void copy(){
    
          int i;
          extern char line[];
          extern char longest[];
    
           i=0;
    
           while((longest[i]=line[i])!='\0')
            ++i;
          }