이봐 요, 미안하지만 많은 질문이 있지만 여기에 어떤 문제가 있는지 잘 모릅니다.이 코드가 실행되지 않는 이유는 무엇입니까?
아래의 C++ 코드에서 사용자 정의 입력 파일을 읽고 출력을 생성합니다. 나는 버그를 해결하기 위해 조각으로 작성하고 컴파일, 테스트 등을하고있다. 이것은 나를위한 학습 경험입니다. 처음에는 자기 주도적 인 프로그램입니다 ...
어쨌든 코드를 실행하면 명령 프롬프트가 하나의 행을 인쇄하고 응답이 없습니다. 나는 그것이 어떤 종류의 고리에서 붙잡 혔다라고 말할 것이다. 그러나 나는 그것이 불가능하다고 생각한다. 나는 그것이 내가 선언하기 위해 노력하고있어 배열과 함께 할 수있는 뭔가가 있다고 생각
, 나는 동적 문자열 배열을 만들고 싶어하지만 난
#include <iostream>
#include <fstream>
#include <algorithm>
#include <cctype>
#include <string>
using namespace std;
int wordCount(string line)
{
int fpos, fpos2;
int count = 0;
fpos = line.find_first_not_of(' ');
line.erase(0, fpos);
while(line.size() > 0)
{
fpos = line.find_first_of(' ');
if(line.at(0) == '"')
{
line.erase(0, 1);
for(int i = 0; i <line.size(); i++)
if(line.at(i) == '"' && line.at(i-1) != '\\')
{
fpos2 = i;
break;
}
line.erase(0, fpos2 + 2);
}
else
line.erase(0, fpos + 1);
count++;
}
return count;
}
int main()
{
//Current line; Input file; Output file;
string currentline, fileName, outFileName;
ifstream fin;
ofstream fout;
cout << "Enter input file name: ";
getline(cin, fileName);
cout << "Enter output file name: ";
getline(cin, outFileName);
fin.open(fileName.c_str());
if (!fin.good()) throw "I/O error";
fout.open(outFileName.c_str());
if (!fout.good()) throw "I/O error";
getline(fin, currentline);
while (!currentline.empty())
{
int pos, pos1;
pos = currentline.find("//");
string postScript = currentline.substr(pos+2,-1);
pos = currentline.find_first_of(';');
string xline = currentline.substr(0,pos+1);
cout << xline << endl;
int size = wordCount(xline);
string *words;
words = (string *) malloc (size*sizeof(string));
words = new string[size];
pos = xline.find_first_not_of(' ');
xline.erase(0, pos);
for (int i = 0; i < size; i++)
{
pos = xline.find_first_of(' ');
if (xline.at(0) == '"')
{
xline.erase(0, 1);
for(int a = 0; a < xline.size(); a++) //This for loop finds the end of a quoted statement within the line.
if (xline.at(a) == '"' && xline.at(a-1) != '\\')
{
pos = a;
break;
}
words[i] = xline.substr(0,pos);
xline.erase(0,pos + 2);
}
else
{
words[i] = xline.substr(0,pos);
xline.erase(0,pos + 1);
}
cout << words[i] << endl;
}
cout << xline << endl << endl;
getline(fin, currentline);
}
return 0;
}
실행 중 여러 지점에서 변수에 무엇이 들어 있는지 보려면'cerr << xxx;'trace 문을 써야합니다. 귀하의 의견을 알지 못하면 프로그램이 실패한 곳을 정확하게 말하기는 어렵습니다. 잠재적 인 버그로 가득차 있습니다. 예를 들어, substr()에는 음의 두 번째 인수가 없어야합니다. find()가 성공했다고 가정합니다. getline()은 입력 성공 여부를 검사해야합니다. 실패 할 경우 현재 행에 의존하지 말고, 코드의 3 분의 1 만 보았습니다. –