problem with getchar EOF

Discussion in 'C Programming' started by pointer, Oct 17, 2009.

  1. pointer

    pointer Guest

    This is a program that collcects input and saves it formated nicely.
    The limit for input is 10 lines.
    When input is more than 10 lines long, message prints and program exits...

    Now, the problem is when there isn't 10 lines...
    My idea was to type the text press Enter, press ctrl+z (is that right
    way to do it???) and that would mean the end of input, and thats when
    getchar() should send EOF to while, and print my container.
    However, i have to press ctrl+z more than once to do that (i think with
    the code bellow two ctrl+z will do the thing)

    Can someone please help me with this? Why is this hapenning?
    I cant realize why doesnt it send EOF first time.

    And the other thing...
    Why do i have to press Enter at the first place, why doesnt it work when
    i press enter in the end of last line...

    Thanks for reading...

    #include <stdio.h>
    #define MAXLINE 10
    #define MAXCHAR 100

    char word[20];
    int length=0;

    int get_line(char line[]);

    char container[MAXLINE][MAXCHAR];
    char line[MAXCHAR];
    int i;
    int count_line;

    while((get_line(line)) > 0)
    for (i=0;i<MAXCHAR - 1 && (line != '\0') ;i++)

    if (count_line==10)
    printf("Sorry, 10 lines is a limit!\n");

    printf("%s", container);
    printf("There are %d lines.", count_line);


    int get_line(char line[])
    int i,j,a,c;

    line=' ';
    for (i = length; i < MAXCHAR - 1 && (c=getchar()) != EOF; ++i)
    if (c==' ' || c=='\t' || c=='\n')
    c=' ';
    line = c;
    if (c==' ')
    else length++;
    for (j=i-length,a=0; j<i && length!=0; j++,a++)
    if (word[0]=='\0')
    line[i-length] = '\0';
    return i;
    pointer, Oct 17, 2009
    1. Advertisements

  2. I can't tell from that description what it should do and the code in
    not easy to read.
    I would not worry about this. This is probably just how your system
    does things and since it is not a C question so there is no C answer.
    It may be possible to set up your system so the EOF is signalled in
    some other way that you like better, but to find you'd have to post a
    question in a group the deals with the OS you are using.
    This is covered in the C FAQ:
    says a little about your original question.
    I was going to comment on the code, but there are a couple of big
    things that might be better to concentrate on instead.

    (1) This name is confusing. The function does not get a line. It may
    get less and it may get more. Also it does some fiddling with another
    array and some counts. A function should do one simple job and it's
    name should explain it as much as possible. You probably want three
    function to the job of this one.

    (2) You link all the code together by using global variables. This
    makes it very had to check things. If a function using only its
    parameters it can be thought of a self-contained unit. If it uses
    globals it usually can't be. See if you can re-write the program
    without word and length being global.

    <snip code>
    Ben Bacarisse, Oct 17, 2009
    1. Advertisements

  3. pointer

    Curtis Dyer Guest

    For some learning exercises, it may be sufficient to just quit upon
    exceeding the maximum length for a line, but it seems more generally
    helpful to retain the flexibility of deciding what to do in the
    calling scope.
    Curtis Dyer, Oct 19, 2009
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.