Hi everybody.

Im trying to make a C program

1 #include<stdio.h>
  2 #define SIZE 100
  3 
  4 int getline(char line[], int lim);
  5 
  6 int getline(char line[], int lim) {
  7     int c,i;
  8 
  9 
 10     for(i=0; i<lim-1 && (c=getchar()) !="\0"; ++i)
 11         line[i] = c;
 12         if(c=="\n") {
 13             line[i]=c;
 14             ++i;
 15         }
 16         line[i]="\0";
 17     return i;
 18 }
 19 
 20 main() {
 21     char line[SIZE];
 22     int c;
 23     int sizeOfArray = sizeof line/sizeof(int);
 24 
 25     while(c=getline(line, SIZE))
 26         printf("%c", (for(int i=0; i<sizeOfArray; i++) line[i]));
 27 }

but when I compile it it gives me this errors

serg@serg-PORTEGE-Z835:~$ gcc hw.c -o hw
hw.c:4:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
hw.c:6:5: error: conflicting types for ‘getline’
/usr/include/stdio.h:671:20: note: previous declaration of ‘getline’ was here
hw.c: In function ‘getline’:
hw.c:10:36: warning: comparison between pointer and integer [enabled by default]
hw.c:12:7: warning: comparison between pointer and integer [enabled by default]
hw.c:16:10: warning: assignment makes integer from pointer without a cast [enabled by default]
hw.c: In function ‘main’:
hw.c:26:17: error: expected expression before ‘for’

Please help me to fix it

I tried looking at your program but had some problems

#include<stdio.h>
#define SIZE 100

int getline(char line[], int lim)
{
  int c,i;

  for( i = 0; i < (lim - 1) && (c=getchar()) != '\0'; ++i)/*characters are surrouned by single quotes*/
	line[i] = c;
  
  if(c == '\n')/*characters are surrouned by single quotes*/
  {
	line[i]=c;
	++i;
  }
  
  line[i] = '\0';/*characters are surrouned by single quotes*/
  return i;
}

int main()/*main return an integer*/ 
{
  char line[SIZE];
  int c;
  int sizeOfArray = sizeof line/sizeof(int);

  while(c == getline(line, SIZE))/*comparion not equals*/
	printf("%c", (for(int i=0; i<sizeOfArray; i++) line[i]));/*have no idea what our doing here*/

  return 0;
}

Thank you so much! You fixed most of the errors! Really appritiate it!

printf("%c", (for(int i=0; i<sizeOfArray; i++) line[i]));/*have no idea what our doing here*/

in here I was trying to print each character from array line[]. Is there any other way to do it?

I made it this way

for(i=0; i<sizeOfArray; i++) {
 27             printf("%c", line[i]);
 28     }

Now my program sucesfully compile, but here is another issue with thisprogram: when Im trying to read some text from a different file (for example ./prog1 < sample.txt) it gives me something like this:

����������������������������������������������������^C
serg@serg-PORTEGE-Z835:~$

and this line keeps increasing until I forced it to stop.
Any sugestions, why it is acting this way?

#include<stdio.h>
#define SIZE 100

int getline(char * line, int lim);

int getline(char * line, int lim) {
  int i;
  char c;

  for(i=0; i<lim-1 && (c=getchar()) !='\0'; ++i) {
    *(line+i) = c;
  }

  /* you already saved c in line[i]
  if(c=='\n') {
     line[i]=c; 
    ++i;
  }*/

  line[++i]='\0';
  return i;
}

main() {
  char line[SIZE];
  int c;

  c=getline(line, SIZE); // getline return the number of caracters saved in line

//you can do this to print a line
  for(i=0;i<c;i++) {
    printf("%c", line[i]);
  }
// or this
    printf("%s", line);
}

But I suggest you to practice with some tutorials to learn about how pointers works

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.