Solved

Blank Line Counter C++

Posted on 2006-10-26
10
223 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
Comment Utility
Okay I think I figured out number 2 but I still need help with 1
0
 
LVL 84

Expert Comment

by:ozo
Comment Utility
if(line[i] == ' ' || line[i] == '\n')

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

Expert Comment

by:_iskywalker_
Comment Utility
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
 

Author Comment

by:xneo27
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:xneo27
Comment Utility
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
Comment Utility
         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
Comment Utility
This gives me all blank lines, I have no clue whats wrong here.
0
 

Author Comment

by:xneo27
Comment Utility
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
Comment Utility
PAQed with points refunded (500)

Computer101
EE Admin
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now