Solved

Blank Line Counter C++

Posted on 2006-10-26
10
231 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

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…
This is about my first experience with programming Arduino.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

777 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