beginner question - unable to view outputs while in a loop

This is a discussion on beginner question - unable to view outputs while in a loop within the C Programming forums, part of the General Programming Boards category; i'm sorry, i have no idea how to word this. i'm learning c independently out of k&r on a mac. ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    5

    beginner question - unable to view outputs while in a loop

    i'm sorry, i have no idea how to word this. i'm learning c independently out of k&r on a mac. but there's a problem.

    so, for example, a very simple program which prints the longest line and length of the line from an input (from k&r)

    Code:
    #include <stdio.h>
    
    #define MAXLINE 1000 /* max input line length */
    
    int getline(char line[], int maxline);
    void copy(char to[], char from[]);
    
    /* print longest input line */
    main()
    {
    	int len, max;
    	char line[MAXLINE], 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;
    }
    
    /* getline; 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: copy 'from' into 'to'; assume 'to' is big enough */
    
    void copy(char to[], char from[])
    {
    	int i;
    
    	while (to[i] = from[i] != '\0')
    		++ i;
    }
    when i compile this and run it by ./a.out, i've got the text input (from the getchar()), but the program never advances to the point where it prints the line and length. i used to be able to put in a break trigger in the getchar loop, but this doesn't work for this program. i suspect that i'm just misunderstanding the concept of EOF, but i have no idea what to do or what to search for (so forgive me if this has been answered before).

    sorry again for a beginner's unknowingly bad wording. thanks for the help in advance.

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    In your read function, i can NEVER be anything less than 1 at the return (s[i] = '\0'; ++i

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    i see. however, how is it possible that i could have less than 1 character in the line? (or alternatively, just getting around this.)

    if it means anything, i'm running in terminal.

  4. #4
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Wait a minute. . . I was miss reading the code -- What I said was wrong.

    How are you interacting with this program? You should be typing in lines of code. When you are done, you should send the EOF code to your program (in Linux this would be by pressing CTRL-D) if you are not redirecting a file from into stdin.

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    this is exactly what i was looking for. thank you.

    (yeah, i'm just typing in random text. hopefully this counts as "lines of code.")

    however, upon sending the EOF code to the above program, i'm still not getting anything printed. hrm.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If I were to guess, stop using CTRL+C and use CTRL+D or CTRL+Z.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Code:
    	while (to[i] = from[i] != '\0')    
    		++ i;
    Don't you feel that you are missing something in while loop when you copy the code form the book?

  8. #8
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    changed

    Code:
    	while (to[i] = from[i] != '\0')    
    		++ i;
    to

    Code:
    	while ((to[i] = from[i]) != '\0')    
    		++ i;
    ahh, got it. problem is solved (or at least is solvable by me now)
    Last edited by aewofij; 07-14-2010 at 01:50 PM.

  9. #9
    Registered User
    Join Date
    Jun 2010
    Location
    Michigan, USA
    Posts
    138
    You have found one of the two things missing from that function.

    Edit: Missed the edit before posting my reply.
    .
    Last edited by pheininger; 07-14-2010 at 02:13 PM.

  10. #10
    Registered User
    Join Date
    Jul 2010
    Posts
    5
    no, i also put i = 0, which solved the problem. hence my previous post. sorry to be unclear.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. newb loop question
    By filio623 in forum C++ Programming
    Replies: 10
    Last Post: 09-18-2009, 02:54 PM
  2. For loop question
    By JuzMe in forum C++ Programming
    Replies: 11
    Last Post: 04-20-2009, 08:39 AM
  3. Loop question
    By kwood965 in forum C Programming
    Replies: 6
    Last Post: 10-29-2008, 11:12 PM
  4. Same old beginner question...
    By Sharmz in forum C Programming
    Replies: 15
    Last Post: 08-04-2008, 11:48 AM
  5. for loop question
    By cdalten in forum C Programming
    Replies: 4
    Last Post: 03-20-2006, 08:42 AM

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