Line Of Code counter - doesn't work
-
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
-
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
-
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
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
-
-
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
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
-
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
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