Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Line Of Code counter - doesn't work

Line Of Code counter - doesn't work

Scheduled Pinned Locked Moved C / C++ / MFC
com
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    XTAL256
    wrote on last edited by
    #1

    Hi, i have been working on an assignment for uni for the past week. Every week we get a new assignment, we create a small program and write a report showing a time log, defect report, etc. This one is a program to count Lines Of Code, in particular, class/object LOC. I couldn't get it working so i submitted the buggy one, but i still need it complete to do the other assignments. Can someone take a look at it and tell me what's wrong, in particular this bit:

    /* Basic trim function, removes start & end whitespace of a line */
    void trim(string& str) {
    string::size_type pos = 0, n1 = 0, n2 = 0;
    while (n1 < str.length() || n2 < str.length()) {
    if ((n2 = str.find('\n', n1)) == string::npos)
    break;
    pos = str.find_last_not_of(" \t", n2);
    if(pos != string::npos && pos > n1) {
    str.erase(pos+1, n2-(pos+1));
    n2 = pos + 1; // Move n2 back to start of deleted bit
    pos = str.find_first_not_of(" \t", n1);
    if(pos != string::npos) {
    str.erase(n1, pos-n1);
    n2 -= pos; // Move n2 back again
    }
    }
    else
    str.erase(n1, n2-n1);
    n1 = n2 + 1;
    }
    }

    which should trim whitespace from the beginning & end of each line + remove any blank lines. I have noticed that the string removal code is also buggy, i don't know what's wrong but it doesn't quite remove everything between " and " Download File[^]. Includes input file

    T J K 3 Replies Last reply
    0
    • X XTAL256

      Hi, i have been working on an assignment for uni for the past week. Every week we get a new assignment, we create a small program and write a report showing a time log, defect report, etc. This one is a program to count Lines Of Code, in particular, class/object LOC. I couldn't get it working so i submitted the buggy one, but i still need it complete to do the other assignments. Can someone take a look at it and tell me what's wrong, in particular this bit:

      /* Basic trim function, removes start & end whitespace of a line */
      void trim(string& str) {
      string::size_type pos = 0, n1 = 0, n2 = 0;
      while (n1 < str.length() || n2 < str.length()) {
      if ((n2 = str.find('\n', n1)) == string::npos)
      break;
      pos = str.find_last_not_of(" \t", n2);
      if(pos != string::npos && pos > n1) {
      str.erase(pos+1, n2-(pos+1));
      n2 = pos + 1; // Move n2 back to start of deleted bit
      pos = str.find_first_not_of(" \t", n1);
      if(pos != string::npos) {
      str.erase(n1, pos-n1);
      n2 -= pos; // Move n2 back again
      }
      }
      else
      str.erase(n1, n2-n1);
      n1 = n2 + 1;
      }
      }

      which should trim whitespace from the beginning & end of each line + remove any blank lines. I have noticed that the string removal code is also buggy, i don't know what's wrong but it doesn't quite remove everything between " and " Download File[^]. Includes input file

      T Offline
      T Offline
      toxcct
      wrote on last edited by
      #2

      Project Line Counter[^] from WndTabs


      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

      X 1 Reply Last reply
      0
      • X XTAL256

        Hi, i have been working on an assignment for uni for the past week. Every week we get a new assignment, we create a small program and write a report showing a time log, defect report, etc. This one is a program to count Lines Of Code, in particular, class/object LOC. I couldn't get it working so i submitted the buggy one, but i still need it complete to do the other assignments. Can someone take a look at it and tell me what's wrong, in particular this bit:

        /* Basic trim function, removes start & end whitespace of a line */
        void trim(string& str) {
        string::size_type pos = 0, n1 = 0, n2 = 0;
        while (n1 < str.length() || n2 < str.length()) {
        if ((n2 = str.find('\n', n1)) == string::npos)
        break;
        pos = str.find_last_not_of(" \t", n2);
        if(pos != string::npos && pos > n1) {
        str.erase(pos+1, n2-(pos+1));
        n2 = pos + 1; // Move n2 back to start of deleted bit
        pos = str.find_first_not_of(" \t", n1);
        if(pos != string::npos) {
        str.erase(n1, pos-n1);
        n2 -= pos; // Move n2 back again
        }
        }
        else
        str.erase(n1, n2-n1);
        n1 = n2 + 1;
        }
        }

        which should trim whitespace from the beginning & end of each line + remove any blank lines. I have noticed that the string removal code is also buggy, i don't know what's wrong but it doesn't quite remove everything between " and " Download File[^]. Includes input file

        J Offline
        J Offline
        John R Shaw
        wrote on last edited by
        #3

        I hope you do not mind, but I am confused. You simply start at the beginning of the line and remove white spaces. Getting the total is more efficient before you remove them, but not necessary. The same can be said for the actual end of the string.. Your code is tight (which I like); my only question is why are you looking for “ \t” (“{space}TAB”)? The goal is to eliminate white space and not a combination of white spaces. I admit I did not look deeper than that, but one flaw at a time. -- modified at 3:25 Friday 23rd March, 2007

        INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

        1 Reply Last reply
        0
        • T toxcct

          Project Line Counter[^] from WndTabs


          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          X Offline
          X Offline
          XTAL256
          wrote on last edited by
          #4

          Thanks, that's good. But i would still like to fix mine and get it working.

          1 Reply Last reply
          0
          • X XTAL256

            Hi, i have been working on an assignment for uni for the past week. Every week we get a new assignment, we create a small program and write a report showing a time log, defect report, etc. This one is a program to count Lines Of Code, in particular, class/object LOC. I couldn't get it working so i submitted the buggy one, but i still need it complete to do the other assignments. Can someone take a look at it and tell me what's wrong, in particular this bit:

            /* Basic trim function, removes start & end whitespace of a line */
            void trim(string& str) {
            string::size_type pos = 0, n1 = 0, n2 = 0;
            while (n1 < str.length() || n2 < str.length()) {
            if ((n2 = str.find('\n', n1)) == string::npos)
            break;
            pos = str.find_last_not_of(" \t", n2);
            if(pos != string::npos && pos > n1) {
            str.erase(pos+1, n2-(pos+1));
            n2 = pos + 1; // Move n2 back to start of deleted bit
            pos = str.find_first_not_of(" \t", n1);
            if(pos != string::npos) {
            str.erase(n1, pos-n1);
            n2 -= pos; // Move n2 back again
            }
            }
            else
            str.erase(n1, n2-n1);
            n1 = n2 + 1;
            }
            }

            which should trim whitespace from the beginning & end of each line + remove any blank lines. I have noticed that the string removal code is also buggy, i don't know what's wrong but it doesn't quite remove everything between " and " Download File[^]. Includes input file

            K Offline
            K Offline
            kakan
            wrote on last edited by
            #5

            It seems to me as this is an excellent opportunity to use the debugger! Just set a breakpoint at the first line of the function, then single-step it. (This is what I would do).

            Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

            X 1 Reply Last reply
            0
            • K kakan

              It seems to me as this is an excellent opportunity to use the debugger! Just set a breakpoint at the first line of the function, then single-step it. (This is what I would do).

              Alcohol. The cause of, and the solution to, all of life's problems - Homer Simpson

              X Offline
              X Offline
              XTAL256
              wrote on last edited by
              #6

              That's a good idea. I used Dev-Cpp's debugger yesterday, but i don't like it all that much. I prefer just inserting cout's to debug. And what i am trying to do with the code it count object lines of code & the LOC of each function in that object. So i need to be able to read function names, which is why i am not just simply removing all whitespace. Also, i realized the other day that i am compiling/running it on Unix as well as Windows so newlines are a problem. I thought getline() just got a line regardless of the format but it actually only uses \n as a delimiter so i might be getting some errors if there are some \r in there too

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups