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;
}
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;
}