[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Blank Line Counter C++

Posted on 2006-10-26
10
Medium Priority
?
236 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Six Sigma Control Plans
Starting up a Project

649 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