Thread: Printing Length of Input and the Limited Input

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dnguyen1022 View Post
    Wouldn't it stop when it gets to a new line or when it reaches EOF due to the while loop that it is in? Also it is suppose to get the length of the REST of the line..since the getline stops whenever it reaches its MAXLINE limit. The main purpose of the program is to print out the length of the longest line, and print out as much as possible of the line as limited by MAXLINE.
    Once you get into the inner while loop, it has to finish before the outer while loop can continue. If you want it to happen once per character, then make it happen once, not an infinite number of times per character.
    Last edited by tabstop; 11-26-2008 at 03:11 PM. Reason: where did for come from?

  2. #17
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    So instead of a while statement, would it be better to use If?
    Or would it be more efficient to change the while loop? Thanks!

    Code:
    if (c != '\n'){
            ++possiblemax;
    }

    Code:
    while((c=getchar()) != '\n'){
            ++possiblemax;
    }

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by dnguyen1022 View Post
    So instead of a while statement, would it be better to use If?
    Or would it be more efficient to change the while loop? Thanks!

    Code:
    if (c != '\n'){
            ++possiblemax;
    }

    Code:
    while((c=getchar()) != '\n'){
            ++possiblemax;
    }
    You might want to change your loop -- currently it stops at EOF, instead of \n.

  4. #19
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    So I changed my code a little bit...I don't think it ever reaches the part where c is equal to a new line within the while loop though. How would I fix this?

    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()) != '\n'){
    				//if c is not equal to a new line
    				if (c != '\n'){
    					//if c is not a new line add one to possible max
    					++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:%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;
    }

  5. #20
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    So now I thought about it again and tried a different approach. My program now doesn't freeze anymore. However it doesn't return anything back...just tells me to press any key to continue.

    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'){
    
    				//if c is not equal to a new line
    				if ((c = getchar()) != '\n'){
    					//if c is not a new line add one to possible max
    					++possiblemax;		 
    				}
    
    				//else if is equal to a new line
    				else if ((c = getchar()) == '\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:%d", realmax + MAXLINE);
    			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;
    }

  6. #21
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    bump..I still need help...

  7. #22
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You do realize the code you've posted doesn't compile? (There is no definition of the symbol "getline" when the function is called, because your prototype is for getLine.)

  8. #23
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Okay..I fixed it..it didn't seem to be having any problems compiling in Microsoft Visual C++ though. I think I know why it did not compile in c++ bloodshed though.

    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)
    		
    		if (len > max){
    			max = len;
    			copy(longest, line);
    		}
    		
    		//checks to see if array ends with a new line
    		if (line[len - 1] != '\n'){
    
    				//if c is not equal to a new line
    				if ((c = getchar()) != '\n'){
    					//if c is not a new line add one to possible max
    					++possiblemax;		 
    				}
    
    				//else if is equal to a new line
    				else if ((c = getchar()) == '\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;
                    }
                }
                                                
    		
    		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;
    }
    Here is the correct code without the spelling mistake. However it still does the same thing..gives me a length only up to 9.

  9. #24
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Code:
    else if ((c = getchar()) == '\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;
                    }
    You realize this will only happen when there are two enter-keys in a row?

  10. #25
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    I am not sure what you mean by two enter keys in a row...can you explain it please? As in press enter twice? I realized I had to do that, but I do not see why my coding would produce such a result.

  11. #26
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, what do you think getchar does if not take characters from input? For this piece of code to execute, the if statement (with a getchar in it) would have to be false, meaning that enter key has been eaten -- but this else if has another getchar in it, which means we need to have another enter-key.

    Edit: Oh: and if you want to go through the whole line, shouldn't you have a while loop (loop until c == \n, rather than just check the very next letter)?
    Last edited by tabstop; 11-28-2008 at 04:40 PM.

  12. #27
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Quote Originally Posted by tabstop View Post
    Code:
    while(c != '\n'){
    	++possiblemax;
    }
    Good luck getting that loop to stop. That's assuming you decide on the name of the function getL/line, since your program doesn't compile as is. And is the point of this c thing in main to throw away the rest of the line?
    Didn't you tell me that loop would never stop? Also I removed that getchar() from my else if, but it seems like I still have to press enter twice.

    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)
    		
    		if (len > max){
    			max = len;
    			copy(longest, line);
    		}
    		
    		//checks to see if array ends with a new line
    		if (line[len - 1] != '\n'){
    				
    				(c = getchar());
    
    				//while c is not equal to a new line
    				while (c != '\n'){
    					//while c is not a new line add one to possiblemax
    					++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){
                                    realmax = possiblemax;
                                    possiblemax = 0;
                    }
    
    				//otherwise reset possiblemax
    					else possiblemax = 0;
                    }
                }
                                                
    		
    		realmax = realmax + max;
    
    		if (max > 0){ //there was a line
    			printf("length:%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;
    }
    It sure does look like it is infinitely going on with this code though. I also moved the (c = getchar()); to the outside of the if and else statements.

  13. #28
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The while and the getchar need to be together, as the only thing that can make your while loop stop is the getchar! You need to change c inside the loop, not ten lines away.

  14. #29
    Registered User
    Join Date
    Nov 2008
    Posts
    110
    Oh I get what you are saying..However now my program does nothing at all, no matter how many "enter-keys" I put in. Assuming my while loop works, I think it may have to do with when my code actually reaches a new line.

    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)
    		
    		if (len > max){
    			max = len;
    			copy(longest, line);
    		}
    		
    		//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){
                                    realmax = possiblemax;
                                    possiblemax = 0;
                    }
    
    				//otherwise reset possiblemax
    					else possiblemax = 0;
                    }
                }
                                                
    		
    		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;
    }
    Also I must say thank you for your patience with me! =]

  15. #30
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your indentation is lying to you, perhaps. Do you want to be able to type in more than one line?
    Code:
    while ((len = getline(line, MAXLINE)) > 0)
    		{
    		if (len > max){
    			max = len;
    			copy(longest, line);
    		}
    		
    		//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){
                                    realmax = possiblemax;
                                    possiblemax = 0;
                    }
    
    				//otherwise reset possiblemax
    					else possiblemax = 0;
                    }
                }
                                   }             
    		
    		realmax = realmax + max;
    
    		if (max > 0){ //there was a line
    			printf("length:%d", realmax);
    			putchar('\n');
    			printf("%s", longest);
    		}
    		return 0;
    }
    Maybe add the pair of braces, so that the while loop sees more than just the reading-in part.

Popular pages Recent additions subscribe to a feed