Solved

Blank Line Counter C++

Posted on 2006-10-26
10
233 Views
Last Modified: 2010-04-16
Okay so heres the code:

void Process()
{
      int counter = 0;
      char line[256];
      bool blank=true;
      
      while (!myfile.eof() )
    {
        blank=true;
        counter++;
      myfile.getline(line,256);
              for (int i=1; i < 257; i++)
            {
               if(line[i] == ' ' || '/n');
            else blank = false;
            }
        if (blank==true) cout << "blankline" << endl;
     
    }  
}//end Reserved

two issues with my current code are:
1. It tells me everyline is blank
2. It seems to count 11 lines where there are 10

Can anyone help me out here?
0
Comment
Question by:xneo27
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
10 Comments
 

Author Comment

by:xneo27
ID: 17815562
Okay I think I figured out number 2 but I still need help with 1
0
 
LVL 84

Expert Comment

by:ozo
ID: 17816019
if(line[i] == ' ' || line[i] == '\n')

eof is set after getline reads the end of the file
0
 
LVL 6

Expert Comment

by:_iskywalker_
ID: 17818403
ozo is right, '\n' is not 0 so it will be always true, so blank=true; you could also
get a problem with the for, in c we count from 0 and not from 1, so
for (int i=0; i < 256; i++)
i didnt know hat you want about the blank (since you go every char in the line you will get also end of line \n, and every line will be blank)
best would be:
void Process()
{
     int counter = 0;
     char line[256];
     bool blank=true;
     
     while (!myfile.eof() )
    {
       blank=true;
       counter++;
      myfile.getline(line,256);
if( line[0]  == '/n');
else{
for (int i=0; i < 256; i++){
             if(isspace(line[i] )) ;
          else {
blank = false;
break;
}
          }
}
       if (blank==true) cout << "blankline" << endl;
     
    }  
}//end Reserved
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:xneo27
ID: 17819543
Okay I tried your code but it doesnt see any blank lines because it looks for spaces and not '/n'. I realize it looks for the '/n' in the first array, but what about the one at the end of a bunch of spaces.
0
 

Author Comment

by:xneo27
ID: 17819617
Besides trying skywalkers code I also use this as suggested by ozo but it too sees no blank lines and i dont know why

void Process()
{
     int counter = 0;
     char line[256];
     bool blank=true;
     
     while (!myfile.eof() )
          {
             blank=true;
             counter++;
            myfile.getline(line,256);
            if( line[0]  == '/n');
            else
                  {
                        for (int i=0; i < 256; i++)
                        {
                              if(line[i] == ' ' || line[i] == '\n');
                              else
                              {
                                    blank = false;
                                    break;
                              }
                        }
                  }
       if (blank==true) cout << "blankline" << endl;
     
          }  
}//end Process
0
 
LVL 84

Expert Comment

by:ozo
ID: 17820743
         myfile.getline(line,256);
          for( int i=0,blank=true; i < 256&&line[i]&&blank; i++){
                        if( line[i] == ' ' || line[i] == '\n' ){
                        }else{
                              blank = false;
                              break;
                         }
             }
0
 

Author Comment

by:xneo27
ID: 17821669
This gives me all blank lines, I have no clue whats wrong here.
0
 

Author Comment

by:xneo27
ID: 17834255
I figured it out own my own:

     while (!myfile.eof() )
    {
             blank=true;
             numLines++;
            getline(myfile,line);
            if(line == "");
            else{blank=false;}
        if (blank==true) numBlank++;
     }

Thanks for everyones assistance though.
0
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 18016344
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about a problem I had getting the GPS LocationListener working.
In this post we will learn different types of Android Layout and some basics of an Android App.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question