0

for some reason it always return 1; lets say char line will have a empty line or not empty line.

int main(void)
{
char line[20];
//user may enter some word or enter nothing in LINE

 if(empty_line(line) != 0)
 {
   //...
 }
}


int empty_line(char line[])
{
  for(i = 0; i < 20; i++)
      {
          if(line[i] == ' ' || line[i] =='\t' || line[i] == '\n')
             {
             }
            else
            {
             return 1;   //not empty
            }
       }
       return 0;  //empty
}

Edited by hwoarang69

2
Contributors
2
Replies
4
Views
5 Years
Discussion Span
Last Post by Ancient Dragon
0
int main(
    )
    {
     while(fgets(line, L_SIZE, finp1) != NULL)
         {
             if(empty_line(line) != 0)                   //ALWAYS GOES INTO THIS IF STATMENTS :(
             {
             //do some thing
             }
         }
    }

Edited by ~s.o.s~: formatting

0

Your first error is assuming there are exactly 20 characters in the line. What if there are 25 spaces? Or only 1 character and it is a space? To solve that problem check for end-of-line terminating character '\0'.

int empty_line(char line[])
{
   int i;
   for(i = 0; line[i] != '\0'; i++)
   {
      if( !isspace(line[i]) && line[i] != '\n')
         return 0; // not an empty line
   }
   return 1; // empty line
}

Edited by Ancient Dragon

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.