Ok I wrote this program a while ago and it just counts the line, now I need to modify it to these requirements:
Count the total program LOC, the total LOC in each object the program contains, and the number of methods in each object. Produce a single LOC count for an entire source program file and separate LOC and method counts for each object. Print out each object name together with its LOC and method count. Also print out the total program LOC count. I assume I would have to do the following to my code but I need a little help
~ read line by line until you get to a { then take the line above it
~ if it says 'class' then take the class name (Standard) and count everything until the matching };
~ if it says :: then take the class name and the function name and count between, and also add to the class total
~if it has no :: then it is a solo function
/* Contains implementation of all functions associated with the Lines Class */
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include "loc.h"
using namespace std;
//Constructor, initializes necessary variables
Lines::Lines()
{
}
//Requests a filename from the user
void Lines::count_lines()
{
char filename[16];
int num_lines = 0; // counter to keep track of lines
char get_next_char; //get the next character
cout << "Enter the name of a file that you would like data for: ";
cin >> filename;
fstream instream(filename);
instream.get(get_next_char);
if(instream.fail())
{
cout << "Input file opening failed.\n";
exit(1);
}
while (! instream.eof()) //checks for end of file
{
if ((get_next_char)== '#')
{
//checks for includes and ifndef, define, endif
num_lines++;
}
else if (get_next_char == ';')
{
//checks for lines with ;
num_lines++;
}
else if (get_next_char == '}')
{
//checks for lines with }
num_lines = num_lines + 3;
}
else
{
}
instream.get(get_next_char);
}
cout << "There are " << num_lines << " lines of code in "
<< "your program." << endl << endl ;
instream.close();
}
/* Header file that defines the Lines Class and it's functions */
#ifndef LOC_H
#define LOC_H
#include <string>
using namespace std;
class Lines
{
public:
Lines(); //Constructor
void count_lines(); //Ask user for input file
};
#endif
/*Main program*/
#include <iostream>
#include <string>
#include "loc.h"
using namespace std;
int main()
{
Lines L1;
L1.count_lines();
return 0;
}