read line

This is a discussion on read line within the C Programming forums, part of the General Programming Boards category; I am trying to read characters and count its numbers. First, my compiler prompted me for the size of the ...

  1. #1
    Registered User
    Join Date
    Apr 2004
    Posts
    26

    read line

    I am trying to read characters and count its numbers. First, my compiler prompted me for the size of the array. I added number 50, why do I have to specify size in this case?
    I have an erro message:
    parse error before "char"
    The line in red is the spot where the error is.


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    int readline(char str[], int n);
    int main()
    {
      char str[50];
      int n;
      
      printf("Enter your name please:\n");
      scanf("%d", str);
      printf("Your name has %d characters", read_line(char str[], int n));  
      int read_line(char str[], int n)
      {
        char ch;
        int i = 0;
        
        while ((ch=getchar()) != '\n')
             if(i < n)
               str[i++]=ch;
               
        str[i]='\0';
        return i;
        }
      
      system("PAUSE");	
      return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    20
    You are trying to use a function declaration where a function call is expected. Also, functions can not be defined inside of other functions.

  3. #3
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Not to step on your toes but here is a working version of your program, only a few minor changes:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int readline(char str[]);
    
    int main()
    {
      char str[50];
     
      printf("Enter your name please:\n");
      scanf("%d", str);
      printf("Your name has %d characters", readline(str));  
     
      system("PAUSE");	
      return 0;
    }
    int readline(char str[])
     {
        char ch;
        int i = 0;
        
        while ((ch=getchar()) != '\n')
               str[i++]=ch;
               
        str[i]='\0';
        return i;
    }
    All in all you didn't do to bad of a job. Basically the program worked, more syntax understanding problems than anything else. BTW what was n for?

    Happy coding!

  4. #4
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    just so your prog doesn't crash and burn
    Code:
        while ((ch=getchar()) && (ch != '\n'))  // while( ((ch = getchar()) != '\0' ) &&  (ch != '\n' ))
        {    
               if(i < (n - 1) )   //assuming n = 50; prevent run-time segmentation fault
                   str[i++]=ch;
        }
    edit: after re-consideration, 'if(i < n)' will suffice granted n = 50
    Last edited by misplaced; 12-24-2004 at 10:00 AM.
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  5. #5
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    nice catch, totally missed that. Now I see what he was doing.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    Quote Originally Posted by andyhunter
    nice catch, totally missed that. Now I see what he was doing.

    you missed scanf("%d") too
    i seem to have GCC 3.3.4
    But how do i start it?
    I dont have a menu for it or anything.

  7. #7
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    I was thinking about that but then I decided what the hell and I compiled it an all worked fine?? Hmm, I wonder if my compiler is pulling a bait and switch.

  8. #8
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    And before anybody else jumps on you about it, I would start learning alternatives to system("PAUSE"). Calls to system() are inherently suspect, because your program's got quite a bit of a problem if that program has been removed by the user. It also makes your code very difficult to port to other systems.

    One alternative is getchar().

  9. #9
    Registered User
    Join Date
    Apr 2004
    Posts
    26
    Thanks guys for your help.

    I compiled program that andyhunter helped me with, and it functions good. It counts characters. I am confused with variable n. Do I really need it? str represents array into which input is stored and n should be the maximum number of characters to be read. If the input is greater than n charachters read_line should discard additional characters. If I make str to be 15 characters and I make macro equals 15 and put that into array is it too much to have if(i<n).

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well, according to your first post, you're passing n along to the function (which they're not doing in the first follow-up reply to your post). We'll assume here that n is to represent the length of the line to read in.

    As such, yes, you do need n, because without it, you have no way of telling when you'll run out off of the end of your array. Consider this:
    Code:
    void unsaferead( char s[] );
    
    int main( void )
    {
        char buf[10]; /* small buffer */
    
        unsaferead( buf ); /* fill it */
    }
    
    void unsaferead( char s[] )
    {
        int i;
        char *p = s;
        while( (i=getchar()) != '\n' )
        {
            *p++ = i;
        }
    }
    Now, because we don't have any method of stopping our buffer, other than someone hitting enter, it will merrily run off the end of your array, trashing whatever is beyond it, likely crashing your program.

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

  11. #11
    Helper
    Join Date
    Jun 2004
    Posts
    255
    Quote Originally Posted by barim
    I am trying to read characters and count its numbers. First, my compiler prompted me for the size of the array. I added number 50, why do I have to specify size in this case?
    I have an erro message:
    parse error before "char"
    Here is a fixed version of your code. Please read it carefully and ask for details if necessary.
    PHP Code:
    #include <stdio.h>
    #include <stdlib.h>

    int read_line (char *strsize_t n)
    {
       
    int ch;
       
    size_t i 0;

    /* All the characters are read. 
     * We don't know that the input line was trucated... 
     */
       
    while ((ch getchar ()) != '\n' && ch != EOF)
       {
          if (
    < (1))
          {
             
    str[i++] = ch;
          }
       }

       
    str[i] = 0;
       return 
    i;
    }

    int main ()
    {
       
    char str[10];
       
    size_t n;

       
    printf ("Enter your name please:\n");
       
    read_line (strsizeof str);

       
    printf ("Your name '%s' has %u characters\n"str, (unsignedn);

       
    system ("pause");
       return 
    0;

    Emmanuel Delahaye

    "C is a sharp tool"

  12. #12
    Registered User
    Join Date
    Apr 2004
    Posts
    26
    I don't understand why you use sizeof as argument, and why in this line if (i < (n - 1)) you need n-1?

    Thanks.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I don't understand why you use sizeof as argument
    sizeof str evaluates to 10. It's a nice easy way to pass the correct size of an array of char even if you decide to change the size for further testing.

    >and why in this line if (i < (n - 1)) you need n-1?
    If you allowed n characters to be read then there wouldn't be any room left for the null character that terminates a string.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "sorting news" assignment
    By prljavibluzer in forum C Programming
    Replies: 7
    Last Post: 02-06-2008, 06:45 AM
  2. OPen a file and read it from the last line
    By c_geek in forum C Programming
    Replies: 14
    Last Post: 01-26-2008, 06:20 AM
  3. easy Q -> read a whole line from file
    By paperbox005 in forum C++ Programming
    Replies: 3
    Last Post: 09-27-2004, 04:58 PM
  4. Read each line of text file.
    By Mithoric in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2003, 12:53 PM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM

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