我在这个while循环中遇到分段错误,我不知道为什么。该程序是一行代码计数器。 while循环用于计算类中的行数,因此只需将'{'添加到堆栈中,并在循环中出现'}'时弹出它们。当堆栈为空时,它应该退出while循环。这是while循环,后跟完整的代码..
int countObjectLines(int index){
stack<char> symbols;
int i = index+1;
int numberLines = 0;
symbols.push('{');
while (!symbols.empty())
{
string test(code[i]);
for(int j = 0; j < test.size(); j++){
if(test[j] == '{'){
symbols.push(test[j]);
}
else if(test[j] == '}' && symbols.top() == '{'){
symbols.pop();
}
else{}
numberLines++;
}
i++;
}
return numberLines;
}
这是完整的代码。
#include <iostream>
#include <fstream>
#include <stack>
#include <string>
#include <vector>
#include <sstream>
using namespace std;
//Lines of Code Iterator Class
class LOCitr{
public:
int count;
vector<string> code;
LOCitr()
{
count = 0;
}
~LOCitr(){}
//Trim White Space so line does not get counted
string trimWhiteSpace(string const& str)
{
size_t first = str.find_first_not_of(' '); //find first character not a space
if(first == string::npos)
return "";
size_t last = str.find_last_not_of(' '); //find the end of line before more whitespaces
return str.substr(first, last-first+1); //create the string again with no white spaces
}
//Trim tab space from string
string trimTab(string const& str)
{
size_t first = str.find_first_not_of('\t');
if(first == string::npos)
return "";
size_t last = str.find_last_not_of('\t');
return str.substr(first, last-first+1); //create the string again with no tab
}
//counter of lines
int counter(string fileName)
{
string line;
string first_word = "";
ifstream myfile (fileName.c_str());
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
line = trimWhiteSpace(line);
line = trimTab(line);
if(line.empty()) //check for empty line
continue;
if(line[0] == '/' && line[1] == '/') //check for comment
continue;
count++;
code.push_back(line);
}
for (int i = 0; i < code.size(); i++){
cout<< endl << "code["<<i<<"] "<<code[i];
stringstream stream(code[i]);
stream >> first_word;
if (first_word == "class" || first_word == "struct")
{
cout << endl << first_word << i;
cout << "objectLines: " << countObjectLines(i);
}
}
myfile.close();
}
else return 0;
return count;
}
int countObjectLines(int index){
stack<char> symbols;
int i = index+1;
int numberLines = 0;
symbols.push('{');
while (!symbols.empty())
{
string test(code[i]);
for(int j = 0; j < test.size(); j++){
if(test[j] == '{'){
symbols.push(test[j]);
}
else if(test[j] == '}' && symbols.top() == '{'){
symbols.pop();
}
else{}
numberLines++;
}
i++;
}
return numberLines;
}
};
int main () {
int count = 0;
string file = "";
LOCitr countMe;
char input = 'y';
//while loop to keep entering files to check
while (input == 'y'){
cout << "Enter file name:" << "\t";
cin >> file;
count = countMe.counter(file);
cout << endl << count <<endl;
cout << "Enter another file? Enter Y/N :" << "\t";
cin >> input;
}
return 0;
}
答案 0 :(得分:0)
您的代码似乎存在一些问题:
您的循环条件为while (!symbols.empty())
,但假设代码中的大括号已正确平衡,则symbols
堆栈不会变空。然后会发生什么?您将code[i]
访问i
超出code
的有效范围。
您的计算numberLines = (j-i)+1;
似乎有误,因为i
用于索引行,而j
用于索引列。不知道如何减去这些并获得任何有意义的东西。