I have written a small code which is supposed to read the console input line and give out the total line number. It gives correct output sometimes & wrong sometimes. It's usually wrong if 1 line contains only 1 char like:
a
b
c
d
There is some mistake but I'm not able to catch what it is. I have tried various combinations of the code, debugging & asking someone else (not this community) to have a look and help me. But all have failed till now. If anyone can point out where I am wrong and what will work, I'll be very grateful.
Here is the code:
#include<stdio.h>
int next_line(){
int cnt=0;
int ch;
ch=getchar();
while (ch!=EOF)
{
ch=getchar();
cnt ++;
if (ch ='\n')
{
break;
}
}
if ((cnt > 0)||(ch ='\n'))
{
return 1;
}
else
{
return 0;
}
}
int read(){
int lines=0;
while (!feof(stdin))
{
next_line();
lines += next_line();
}
return lines;
}
int main(){
int n=0;
n=read();
printf("\n%d",n);
return 0;
}
thank you in advance