Thread: Printing Length of Input and the Limited Input

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    line[MAXLINE]
    this is out of bounds access, the valid indexes are from 0 to MAXLINE - 1
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  2. #2
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Hmm...alright I'll change it around and see what I get, with the MAXLINE - 1 in mind. Also
    silverlight, I apologize if this sounds rude, but I am trying to follow the book and using the information that they have given me so far to solve this problem. I believe I should solve this problem with the basics before getting into more complicated stuff or by using shortcuts. I appreciate your help and still want your help to solve this problem but in a less advanced way...for now.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    So..I tried it again...this time my code does not seem to be doing anything. I think I have the concept down. However my code is wrong....I think the
    Code:
    (c = getchar())
    must be intefering with my getline function...can somebody help 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 end of file
    			while ((c = getchar()) != EOF){
    				//if c is not equal to a new line
    				if (c != '\n'){
    					//while c is not a new line add one to possible max
    					while(c != '\n'){
    						     ++possiblemax;
    							 }
    				}
    
    				//else if is equal to a new line
    				else if (c == '\n'){
    					//if possiblemax is greater than real max
    					//set realmax to possiblemax
    					//reset possible max
    					if (possiblemax > realmax){
                                    realmax = possiblemax;
                                    possiblemax = 0;
                    }
    
    				//otherwise reset possiblemax
    					else possiblemax = 0;
                    }
                }
            }
                                                
    		if (len > max){
    			max = len;
    			copy(longest, line);
    		}
    
    		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;
    }

Popular pages Recent additions subscribe to a feed