Thread: Square appearing

  1. #1
    Registered User
    Join Date
    Sep 2015
    Posts
    12

    Square appearing

    Hello guys,
    when i built the program using Codeblocks and write "aaa" and then hit "Enter" the output was like: "4: aaa" and there was a square standing on its vertex just below "4". How did it hapen?
    The code is here:
    Code:
    #include <stdio.h>
    
    #define MAXLINE 4 /* maximum input line size */
    
    
    int getline(char line[], int maxline);
    void copy(char to[], char from[]);
    
    
    /* print longest input line */
    int main(void)
    {
      int len;               /* current line length */
      int max;               /* maximum length seen so far */
      char line[MAXLINE];    /* current input line */
      char longest[MAXLINE]; /* longest line saved here */
    
    
      max = 0;
    
    
      while((len = getline(line, MAXLINE)) > 0)
      {
        printf("%d: %s", len, line);
    
    
        if(len > max)
        {
          max = len;
          copy(longest, line);
        }
      }
      if(max > 0)
      {
        printf("Longest is %d characters:\n%s", max, longest);
      }
      printf("\n");
      return 0;
    }
    
    
    /* getline: read a line into s, return length */
    int getline(char s[], int lim)
    {
      int c, i, j;
    
    
      for(i = 0, j = 0; (c = getchar())!=EOF && c != '\n'; ++i)
      {
        if(i < lim - 1)
        {
          s[j++] = c;
        }
      }
      if(c == '\n')
      {
        if(i <= lim - 1)
        {
          s[j++] = c;
        }
        ++i;
      }
      s[j]='\0' ;
      return i;
    }
    
    
    /* copy: 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;
      }
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by shotakobakhidze
    when i built the program using Codeblocks and write "aaa" and then hit "Enter" the output was like: "4: aaa" and there was a square standing on its vertex just below "4". How did it hapen?
    Are you sure that is not just the cursor for you to enter the next line of input?

    By the way, why do you have both i and j in the implementation of getline? It seems to me that it would be simpler to use just one of them. Also, note that getline is the name of a POSIX function, so it may be safer to use a different name or add a prefix, e.g., shota_getline.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Sep 2015
    Posts
    12
    if the amount of written characters isn't 3(without hitting enter), then nothing appears after hitting "Enter" and the cursor stands below "the number of characters", but if the amount is 3(in the example above), then the square appears and the cursor next to it.

  4. #4
    Registered User Ktulu's Avatar
    Join Date
    Oct 2006
    Posts
    107
    Because you have MAXLINE defined as 4.
    This parameter is reserved

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    I think you have to return j not i.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You also need to assign max to a value other than zero.

    And what happens if the user enters EOF?

    Jim

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help...draw a square within a square pattern
    By gzmiido in forum C++ Programming
    Replies: 6
    Last Post: 10-27-2013, 10:18 AM
  2. Garbage appearing
    By DeanWinchester in forum C Programming
    Replies: 3
    Last Post: 05-29-2012, 03:21 AM
  3. Garbage appearing
    By DeanWinchester in forum C Programming
    Replies: 3
    Last Post: 05-27-2012, 02:47 PM
  4. line-square and circle-square intersections
    By tomast in forum Game Programming
    Replies: 1
    Last Post: 11-16-2008, 08:12 AM
  5. for loops using square roots, square, Cube
    By lotf in forum C Programming
    Replies: 3
    Last Post: 03-21-2004, 04:29 AM