Hello, first time posting, thank you for your efforts in advance. I have a program right now that counts all of the lines of code excluding comments and blank lines. I was wondering how I would get it to count the number of methods and Object LOC? Any help would be appreciated
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
usingnamespace std;
class LOC{
public:
LOC ( string fname ); // constructor
void countLines();
int returnNumber(); // will return the numLines, needed because numLines is a private variable
private:
string line, filename;
int numLines;
};
LOC::LOC( string fname )
{
filename = fname;
}
int LOC::returnNumber()
{
return numLines;
}
void LOC::countLines()
{
ifstream myfile( filename.c_str() );
while (getline(myfile, line))
{
if(line[0] == '/' || line[0] == '\0' ) //ommit lines that are blank or have '/' at beginning
{
numLines--;
}
numLines++; //increments counter of number of lines
}
}
int main()
{
string fname = "main.cpp";
LOC bo(fname); // this will create an LOC class, the constructor will fire up and will assign fname to bo’s ‘filename’ string.
bo.countLines();
cout << "The number of lines of code is: " << bo.returnNumber() << endl;
return 0;
}
Okay I have the program setup, and it runs but it puts out huge numbers for total LOC and methods, I need some help with why this is happening please and thank you.
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
usingnamespace std;
class LOC{
public:
LOC ( string fname ) // constructor
{
filename = fname;
}
int returnNumber() // will return the numLines, needed because numLines is a private variable
{
return numLines;
}
int returnObjs()
{
return objlines;
}
int returnMethod()
{
return methodLines;
}
void countLines()
{
ifstream myfile( filename.c_str() );
while (getline(myfile, line))
{
if(line[0] == '/' || line[0] == '\0') //ommit lines that are blank or have '/' at beginning
{
numLines--;
}
if (line[0] != 'LOC')
{
objlines--;
}
if(line[0] != 'void' || line[0] != 'int' && line[1] == 'r')
{
methodLines--;
}
numLines++; //increments counter of number of lines
objlines++;
methodLines++;
}
}
private:
string line, filename;
int numLines, methodLines, objlines;
};
/*LOC::LOC( string fname )
{
filename = fname;
}
int LOC::returnNumber()
{
return numLines;
}
/*int LOC::returnObjs()
{
return objlines;
}
int LOC::returnMethod()
{
return methodLines;
}
void LOC::countLines()
{
ifstream myfile( filename.c_str() );
while (getline(myfile, line)) // Are we going to still need the bo.line?
{
if(line[0] == '/' || line[0] == '\0') //ommit lines that are blank or have '/' at beginning
{
numLines--;
}
if (line[0] != 'LOC')
{
objlines--;
}
if(line[0] != 'void' && line[1] != 'LOC' || line[0] != 'int' && line[1] == 'LOC')
{
methodLines--;
}
numLines++; //increments counter of number of lines
objlines++;
methodLines++;
}
}
*/
int main()
{
string fname = "prog3a.cpp";
LOC bo(fname); // this will create an LOC class, the constructor will fire up and will assign fname to bo’s ‘filename’ string.
bo.countLines();
cout << "The Total number of lines of code is: " << bo.returnNumber() << endl;
cout << "The number of objects is: " << bo.returnObjs() << endl;
cout << "The number of methods is: " << bo.returnMethod() << endl;
return 0;
}
int returnNumber() // will return the numLines, needed because numLines is a private variable
{
return numLines;
}
int returnObjs()
{
return objlines;
}
int returnMethod()
{
return methodLines;
}
Okay, well now I am getting the code to print correctly the total number of lines, but it is printing every single line instead of omitting comments '/' or empty line '\0' whats wrong with my if statement?
#include <iostream>
#include <string>
#include <fstream>
#include <stdio.h>
usingnamespace std;
class LOC{
public:
LOC ( string fname ) // constructor
{
filename = fname;
}
int returnNumber() // will return the numLines, needed because numLines is a private variable
{
return numLines;
}
void countLines()
{
ifstream myfile( filename.c_str() );
while (getline(myfile, line))
{
if(line[0] == '/' || line[0] == '\0') //ommit lines that are blank or have '/' at beginning
{
numLines--;
}
else
{
numLines++; //increments counter of number of lines
}
}
}
private:
string line, filename;
int numLines;
}; // class ends
int main()
{
string fname = "prog3a.cpp";
LOC bo(fname); // this will create an LOC class, the constructor will fire up and will assign fname to bo’s ‘filename’ string.
bo.countLines();
cout << "The Total number of lines of code is: " << bo.returnNumber() << endl;
return 0;
}
class LOC{
public:
LOC ( string fname ) // constructor
{
numLines = 0;
filename = fname;
}
I am still only getting total lines of code without omitting the comments or blank lines. I do not want to count comments or blank lines as code but it still is unfortunately.
Comments rarely occur at the very beginning of a line. You do not count blank lines (however, you would count lines that have at least one non-newline white space character.) In fact, you reduce the line count for empty lines.
1 2 3
{
}
would result in a line count of 1 with the last code you posted.
class LOC{
public:
LOC ( string fname ) // constructor
{
filename = fname;
}
int returnNumber() // will return the numLines, needed because numLines is a private variable
{
return numLines;
}
void countLines()
{
ifstream myfile( filename.c_str() );
while (getline(myfile, line))
{
if(line[0] == '/' || line[0] == '\0') //ommit lines that are blank or have '/' at beginning
{
numLines--;
}
else
{
numLines++; //increments counter of number of lines
}
}
}
private:
string line, filename;
int numLines;
};
How would I do this? Because I am trying to check for the number of objects, how many lines total are those objects, and the number of methods, with there number of lines total... I am starting to see code when I sleep lol.