C++에서 아무 것도하지 않았으므로 시간이 지났습니다. while while 루프에서 매우 이상한 동작이 나타납니다. 사용자가 무한한 수의 명령을 실행할 수 있도록 설계되었으며 문자열의 내용으로 실행할 명령을 결정합니다.while 루프는 메서드 완료를 기다리지 않습니다.
여기에 관심있는 코드입니다 :
string run;
size_t found;
bool understood;
while (true)
{
run = "";
found = string::npos;
cout << "Please enter command(s)" << endl;
cout << "\> ";
cin >> run;
found = run.find("convert");
cout << found << endl;
understood = false;
if (found != string::npos)
{
cout << "Converting DNA" << endl;
understood = true;
convert();
}
found = run.find("purify");
if (found != string::npos)
{
cout << "Purifying DNA" << endl;
purify();
understood = true;
}
found = run.find("build");
if (found != string::npos)
{
cout << "Building overlaps" << endl;
buildOverlaps();
understood = true;
}
found = run.find("close");
if (found != string::npos)
{
cout << "Goodbye" << endl;
break;
}
if (understood == false) cout << "I'm sorry, I didn't understand you" << endl;
}
난 그냥 경우 관련 내 원래의 방법에서 이동 문자열 == "문자열"여러 명령은 같은 라인으로 실행할 수 있도록. 나는이 새로운 코드를 실행할 때, 나는 그래서 기본적으로
Please enter command(s)
> run converter
(some long, nonzero, number)
I'm sorry, I didn't understand you
Please enter command(s)
> 0
Converting DNA
을 얻을, 문자열에 걸릴 블록의 경우 (마지막 제외)을 건너 뛰고 다음 주위에 다시 포장하고 적절한 방법을 실행하기 위해 나타납니다. 그것은 모든 것이 효과가 있으므로 사소한 성가심 일뿐입니다.하지만 그 행동을 이해하고 싶습니다.
숫자는 발견 된 문자열 색인의 디버그 출력이며 테스트 실행이 아닌 경우에는 표시되지 않습니다.
입력란에 'run converter'라고 입력해야합니까? 'cin >> run'에 대한 한 번의 호출로 전체 문자열 "run converter"를 얻으려고하십니까? 스트림 추출 연산자 ('<<')는 공백에서 멈 춥니 다. 전체 문자열을 원하면'std :: getline (cin, run);을 사용하십시오. – Chad