Printing Length of Input and the Limited Input

This is a discussion on Printing Length of Input and the Limited Input within the C Programming forums, part of the General Programming Boards category; wow..thanks...so now I am able to get the correct length of the longest line, but it does not copy the ...

  1. #31
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    wow..thanks...so now I am able to get the correct length of the longest line, but it does not copy the correct longest line. it simply gives me back the first longest line that passes MAXLINE. However It does give me back the correct one when all the given lines have a length less than MAXLINE. I think something may be wrong with the way im copying it.

    Edit: So I fixed it..however I don't see why the copy inside the while loop does not interfere with the one outside of it. I would see why it wouldnt if all the lines were within MAXLINE because it would never reach the point where it adds to possiblemax. However when there are a mixture of lines, the code seems to run fine. I would think the copy outside the while loop would interfere with the one inside. Can someone explain this to me please?

    Code:
    #include <stdio.h>
    #define MAXLINE 10
    
    int getline(char line[], int maxline);
    void copy(char to[], char from[]);
    
    //prints longest input line
    main(){
    	int len; //current line length
    	int max; //maximum length seen so far
    	int possiblemax; //possible maximum length
    	int realmax; //the real maximum length
    	int c; //the char
        char line[MAXLINE]; //current input line
    	char longest[MAXLINE]; //longest line saved here
    	
        max = possiblemax = realmax = 0;
    
    	while ((len = getline(line, MAXLINE)) > 0){
    
    		//checks to see if array ends with a new line
    		if (line[len - 1] != '\n'){
    
    				//while c is not equal to a new line
    				while ((c = getchar()) != '\n'){
    					//while c is not a new line add one to possible max
    					++possiblemax;
    				}
    
    				//if is equal to a new line
    				if (c  == '\n'){
    
    					//if possiblemax is greater than real max
    					//set realmax to possiblemax
    					//reset possible max
    					if (possiblemax > realmax){
    								copy(longest, line);
                                    realmax = possiblemax;
                                    possiblemax = 0;
                    }
    
    				//otherwise reset possiblemax
    					else possiblemax = 0;
                    }
                }
    
    		if (len > max){
    			max = len;
    			copy(longest, line);
    		}
    	}
                                                
    		
    		realmax = realmax + max;
    
    		if (max > 0){ //there was a line
    			printf("length:&#37;d", realmax);
    			putchar('\n');
    			printf("%s", longest);
    		}
    		return 0;
    }
    
    //read a line into s, return length
    int getline(char s[], int lim){
    	int c, i;
    
    	for(i = 0; i < lim-1 && (c = getchar()) != EOF && c != '\n'; ++i)
    		s[i] = c;
    	if(c == '\n'){
    		s[i] = c;
    		++i;
    	}
    	s[i] = '\0';
    	return i;
    }
    
    //copy from into to: assume to is big enough
    void copy(char to[], char from[]){
    	int i;
    
    	i = 0;
    	while ((to[i] = from[i]) != '\0')
    		++i;
    }
    Last edited by dnguyen1022; 11-28-2008 at 10:37 PM.

  2. #32
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    What do you mean by interfere with? You have a big if-else at the top -- if the line is long, you go into the "if" part, if it's short it goes into the "else" part. If the line you have is longer than the previous longest line, you copy it. It overwrites what was previously there.

  3. #33
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    What I meant was if say the program does reach a line that is longer than MAXLINE, the inside copy inside the FIRST IF copies the line into the array. What would the second copy within the SECOND IF do?


    Quote Originally Posted by dnguyen1022 View Post

    Code:
                                     //FIRST IF
    		//checks to see if array ends with a new line
    		if (line[len - 1] != '\n'){
    
    				//while c is not equal to a new line
    				while ((c = getchar()) != '\n'){
    					//while c is not a new line add one to possible max
    					++possiblemax;
    				}
    
    				//if is equal to a new line
    				if (c  == '\n'){
    
    					//if possiblemax is greater than real max
    					//set realmax to possiblemax
    					//reset possible max
    					if (possiblemax > realmax){
                                                                                                                            
    		                   copy(longest, line);
                                    realmax = possiblemax;
                                    possiblemax = 0;
                    }
    
    				//otherwise reset possiblemax
    					else possiblemax = 0;
                    }
                }
                                    //SECOND IF
    		if (len > max){
    			max = len;
    			copy(longest, line);
    		}
    	}
    Also there actually is no else if. I noticed if I changed the SECOND IF to an else if, the code will not work if it reaches a line that is over the MAXLINE limit.
    Last edited by dnguyen1022; 11-29-2008 at 01:21 PM.

  4. #34
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Then in your current code, your second copy statement is what saves your bacon -- notice that max is not set to the proper value in the first copy, so it will immediately get copied again in the second copy (since max is still at its prior, smaller, value)

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21