problem with getchar EOF

P

pointer

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[]);

main()
{
char container[MAXLINE][MAXCHAR];
char line[MAXCHAR];
int i;
int count_line;
count_line=0;

while((get_line(line)) > 0)
{
for (i=0;i<MAXCHAR - 1 && (line != '\0') ;i++)
{
container[count_line]=line;
}
container[count_line]='\0';
count_line++;

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

}
for(i=0;i<count_line;i++)
{
printf("%s", container);
printf("\n");
}
printf("There are %d lines.", count_line);

}



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

for(i=0;i<length;i++)
line=word;
line=' ';
word[0]='\0';
for (i = length; i < MAXCHAR - 1 && (c=getchar()) != EOF; ++i)
{
if (c==' ' || c=='\t' || c=='\n')
c=' ';
line = c;
if (c==' ')
length=0;
else length++;
}
for (j=i-length,a=0; j<i && length!=0; j++,a++)
word[a]=line[j];
if (word[0]=='\0')
line='\0';
line[i-length] = '\0';
return i;
}
 
B

Ben Bacarisse

pointer said:
This is a program that collcects input and saves it formated nicely.

I can't tell from that description what it should do and the code in
not easy to read.
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.

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.
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...

This is covered in the C FAQ:
http://c-faq.com/osdep/cbreak.html

http://c-faq.com/stdio/eofval.html
says a little about your original question.
#include <stdio.h>
#define MAXLINE 10
#define MAXCHAR 100

char word[20];
int length=0;

int get_line(char line[]);

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>
 
C

Curtis Dyer

The OP obviously had a learning exercise to complete, rather
than a real problem to solve.
For a real app you would probably use fgets(), I agree. (However
getline() is easier to use since it terminates on overlong input
lines).

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top