-2

im looking to count the number of lines in a code i have figured that i need to look for a specific character from the file and increment by one each time that it is found. Now i have a general gist of it but im having trouble figuring out how to read each line in the file. I have posted what i have so far. Any help would be appreciated

#include<fstream>
#include <iostream>

using namespace std;

 int main()
{
   ifstream readfile;
   readfile.open("project1b.cbp");
   int number_of_lines=0;
   char = ch;
   while(ch !=EOF)
   {
       if(ch!='endl'||';')
        number_of_lines++;
   }


    return 0;
}
MoMo8
  • 25
  • 5
  • 1
    `while(std::getline(string, file)) n_lines++;` -- btw, why is this tagged C? –  Jan 27 '14 at 20:40
  • 1
    Is this part of an exercise? If not I would suggest taking a look at http://www.dwheeler.com/sloccount/ – Martin Jan 27 '14 at 20:46

3 Answers3

2

The line

   if(ch!='endl'||';')

does NOT do what you think. You are checking that ch is not equal to endl, or that ; is. And since ; is always true (not zero), you are not really having a very useful comparison here.

You might be thinking of

   if(ch!='endl'|| ch!=';')

Or something like that. Not sure what you are trying to achieve with your comparison...

As for the other part of your question - the top Google hit for "C++ read file line by line" gets you to the following stackoverflow answer:

https://stackoverflow.com/a/7868998/1967396

It's all you need.

One more thing - you need to define more clearly for yourself what a "line of code" is. Is basically "any carriage return, or any semicolon" a new line? What about semicolon followed by new line? What about a line with two semicolons (two statements on one line)?

It will take a bit more puzzling on your part to clear up your thinking. I recommend that you split the logic into

  1. Get a line from the file
  2. Count semicolons
  3. If there are semicolons, is there anything else of note after them
  4. Decide how to count these

Final thought: the single quote ' ' is used to define a character constant. The double quote " " is used to define a string constant. The end of line character is usually \n or \r; in C++ we use std::endl as a way to write that - but it's not in quotes. And when you use namespace std presumably you can just write endl when you mean '\n'. But putting single quotes around it confuses me - and probably the compiler too.

Community
  • 1
  • 1
Floris
  • 45,857
  • 6
  • 70
  • 122
  • ah i see. what i am trying to get is that if i have either one of these chars in my line that it counts as a line and to increment the number of lines counter by one. – MoMo8 Jan 27 '14 at 20:44
  • 2
    @user3006893 and what do you expect `'endl'` to be? Did you even read any documentation? It's a random multi-character string literal, it's not something you get when you reach the end of a line (you get `'\n'` or `'\r'` when you do). –  Jan 27 '14 at 20:47
0

You can use std::getline for getting each line.

Sujith Surendranathan
  • 2,569
  • 17
  • 21
0

Try this:

#include <fstream>
#include <iostream>
using namespace std;

int main(int argc, char **argv){
  if(argc!=2){
    cout<<"Syntax: "<<argv[0]<<" <FILENAME>"<<endl;
    return -1;
  }

  ifstream readfile(argv[1]);
  if(!readfile.good()){
    cout<<"Failed to open file: '"<<argv[1]<<"'!"<<endl;
    return -1;
  }

  int number_of_lines=0;
  while(readfile.good()){
    char ch=readfile.get();
    if(ch=='\n'|| ch==';')
      number_of_lines++;
  }

  cout<<"Number of lines: "<<number_of_lines<<endl;

  return 0;
}

Some differences from your code.

  • I use argc and argv so that the user can specific a file name on the command line.
  • I load the file with ifstream readfile(argv[1]). This fits the RAII (Resource Acquistion is Initialization) scheme commonly used in C++. I don't use a close() statement because the ifstream will go out of scope and automatically clean itself up. Using RAII makes your code cleaner and less-prone to errors.
  • I use get() to acquire new characters from the character stream. This was missing in your code.
  • The end of the line occurs when the \n (newline) character is the one we're currently looking at, or if we are looking at the ; character. endl is not a character constant - it's more of a stream manipulator - so you cannot use it in the manner you do, nor to do comparisons of this sort in general.
  • Note that the or operator (||) divides two complete logic statements, the way in which you use it ((ch!='endl'||';')) does not work the way you think it does.
  • I opt to use K&R style bracing rather than your heathen Allman/GNU style. (This is something of a joke, but I encourage you to try several different styles and find one that's right for you. I prefer K&R for SO because the code is more compact.

You should ask yourself if this is really the way you want to count lines of code. For instance, a for construct (for(int i=0;i<N;++i)) will end up looking like three lines of code using your metric.

Richard
  • 56,349
  • 34
  • 180
  • 251
  • thank you for your help. the program you provided gave me a push into the right direction. I do not really know of the K&R style as I am I student we are a basically left in a sense to our own devices. But I kind of picked up the style you mentioned due to the fact that it feels most logical to me. I can see all of my braces with out have to worry about missing one. I think this was something that i developed on my own. I do have a question on if you had any tips on becoming a better programmer like yourself. I sometimes feel as if my school has failed me. Or is this a common for the noobs? – MoMo8 Feb 03 '14 at 20:54
  • I feel as if my programs that I make they work but they are very short. That could just be a testament to my style of thinking and thinking of making everything as simple as possible? – MoMo8 Feb 03 '14 at 20:55
  • Developing an aesthetic sense of how your code should look is useful: indentations and brackets. I like the K&R style because I rarely forget brackets and therefore want them to be unobtrusive. When I started, I wanted to learn how to program games. Figure out how to code a "Guess the Number" game, try to code [Hunt the Wumpus](https://en.wikipedia.org/wiki/Hunt_the_Wumpus). Always start with a simple version of the game then add elements to make it more complicated. – Richard Feb 03 '14 at 21:09