program to compare C to C++

I know I put a post up before on this but here is all the rest so I have an assignment were we have to have the program look at some lines of code and pick out certain things. Now from some help I have the beginning where the program can view the file but unsure who I can get it to pull out the rest

This is what I have so far
1
2
3
4
5
6
7
8
9
 main(){
std::ifstream ifs ("Example.code");

while (ifs){
std::string line;
std::getline(ifs, line);
}
return 0
}


Now I wasn't sure if I should put this code in the main or put it in a different function but right now I'm thinking that keeping it in the main might work the best.

Now the rest I'm thinking that if I put the functions into a class since they are similar that should work.

Let me know what you think. These are the info that I need to pull from a text file

Number of total lines in the file – you need this for percentages.
Percentage of blank lines
Percentage of statements, as figured by counting semicolons (;)
Percentage of blocks, as figured by counting braces ({ and })
Percentage of pointer references, as figured by counting “->”
Percentage of comment lines, including both // and /* / style comments. Bear in mind that either type of comment can appear in either type of file, and that you should count each line that occurs between a /* and a */ as a comment line.
As a next step, try writing functions that indicate if the line is any of these things. Don't do the comment lines yet - that's the hardest one. For example:
1
2
bool isBlankLine(const string &line);
bool is statementLine(const string &line);

etc.

Notice that a single line can be a statement line, a block line, a pointer reference line and a comment line:
if (x->y) {a=1; b=2} // now isn't that special
So you will want to test the conditions and increment the counters independently for these.

Be a hero and handle the case where comments contain code:
x=1; // Statement line, even though it has -> and { }

You could handle // comments by having isSlashSlash() remove the comment:
1
2
3
4
bool isSlashSlash(string &line)
{
   // If line contains "//" then remove everything from // to end of line and return true
}


Now if you call this one first, you won't have to worry about what was in the comment:
1
2
3
if (isBlank()) ++blankLines;
if (isSlashSlash(line)) ++commentLines;
if (isStatementLine(line) ++statementLines;

etc.

The wrench in the works is the /* ... */ comments. You could have isSlashStar() delete everything from /* to */, reading additional lines as you go. Be sure to increment the total number of lines if you do this.

Good luck,
Dave

Topic archived. No new replies allowed.