Solved

Blank Line Counter C++

Posted on 2006-10-26
10
232 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
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

829 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