Program not working

Hello guys,

I have this program, which is supposed to read a set of text lines and print the longest. However I can`t get it to print anything. Any suggestions, or is the program just dumb?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

#include <stdio.h>
#define MAXLINE 1000     /* maximum input line size */

int getline(char line[ ], int maxline);
void copy(char to[ ], char from[ ]);

/* print longest input line */

int getline(char line[ ], int maxline[ ]);

 

int main()
{
	int len;
	int max;
	char line[MAXLINE];
	char longest[MAXLINE];
	
	max = 0;
	while((len = getline(line, MAXLINE)) > 0)
		if (len > max)  {
		max = len;
		copy(longest, line);	
			
		}
	
		if (max > 0)
			printf("%s", longest);
		return 0;
	
	}
	
	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;
	}
	
	
	
	void copy (char to[ ], char from[ ])
	{
		int i;
		
		i = 0;
		while ((to[i] = from[i]) != '\0')
		++i;
		
	}	
		










Hey pacman169, two problems:

1. You don't have any {} around your while loop, it will only ever execute the first if statement.

2. Your program will always quit after 1 entry when you fix problem 1.
Hey pacman169, two problems:
1. Your identation sucks.
2. You don't know how to terminate your input. (<C-d> for *nix, you may also use stream redirection)
Last edited on
Is it now working?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include <stdio.h>
#define MAXLINE 1000     /* maximum input line size */

int getline(char line[ ], int maxline);
void copy(char to[ ], char from[ ]);

/* print longest input line */

int getline(char line[ ], int maxline[ ]);

 

int main()
{
	int len;
	int max;
	char line[MAXLINE];
	char longest[MAXLINE];
	
	max = 0;
	while((len = getline(line, MAXLINE)) > 0) {
		if (len > max)  {
		max = len;
		copy(longest, line);	
			
		}
       }
	
		if (max > 0) {
                    while(feof(stdin)) {
                    
		     printf("%s", longest);
		     return 0;
                }
	
	      }
}
	
	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;
	}
	
	
	
	void copy (char to[ ], char from[ ])
	{
		int i;
		
		i = 0;
		while ((to[i] = from[i]) != '\0')
		++i;
		
	}	
		



In my tests everything worked so far.
your indentation remains awful.

1
2
3
4
while(feof(stdin)) { //useless check
   printf("%s", longest);
   return 0; //unconditional termination
}
¿what do you think you are doing there?


Also, ¿why do you add the delimiter character to the line?
1
2
3
4
if (c == '\n') {
   s[i] = c;
   ++i;
}
One way to test for the end of the stream is with the feof function

http://www.cplusplus.com/reference/cstdio/feof/

" Also, ¿why do you add the delimiter character to the line? "


I don`t understand the question. I think the program is supposed to count the lengths of the strings including whitespace, and when newline is hit, restart counting.

The next step is to count the lengths of arbitrarily long input lines, and output as number.
The loop in line 22 would end in the case of eof, no need to check again.
There is no point in using a while loop in you are not going to do any looping.


¿Is the line break character part of the line? like "hello" is actually "hello\n"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

#include <stdio.h>
#define MAXLINE 1000     /* maximum input line size */

int getline(char line[ ], int maxline);
void copy(char to[ ], char from[ ]);

/* print longest input line */

int getline(char line[ ], int maxline[ ]);

 

int main()
{
	int len;
	int max;
	char line[MAXLINE];
	char longest[MAXLINE];
	
	max = 0; 
	while((len = getline(line, MAXLINE)) > 0) {
	    if (len > max)  {
		max = len;
		copy(longest, line);	
			
            }
         }
               
	
		if (max > 0) {   /* there was a line */

                   
                     printf("\n Longest line: ");
		     printf("%s", longest);

                     printf("\n Length of line:");
		     printf("%d", max);
                     printf("\n");
		     return 0;
                
	
	      }
}
	
	int getline(char s[ ], int lim)
		
	{
		int c, i;
		
		for (i=0; i<lim-1 && (c = getchar()) != EOF && c!= '\n'; ++i)
		s[i] = c;
		
		s[i] = '\0';
		return i;
	}
	
	
	
	void copy (char to[ ], char from[ ])
	{
		int i;
		
		i = 0;
		while ((to[i] = from[i]) != '\0')
		    ++i;
		
	}	
		



It was as simple as that ;)
Topic archived. No new replies allowed.