为什么当我键入stop时while循环没有停止

  • 本文关键字:循环 while stop c++
  • 更新时间 :
  • 英文 :


我在类似的c++中有一个while循环

bool stopped = false;
while (!stopped) {
string input;
cin >> input;
if (input == "stop") {
stopped = true;
}
}

当我使用停止时,它不会停在只有空行的地方

我的完整代码是

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
bool stopped = false;
ofstream script;
string scriptname;
getline(cin >> ws, scriptname);
script.open(scriptname);
while (!stopped) {
string input;
cin >> input;
if (input == "line") {
string lineInput;
getline(cin >> ws, lineInput);
int i = lineInput.find(',');
lineInput[i] = ':';
script << lineInput << endl;
} else if(input == "wait") {
string waitInput;
getline(cin >> ws, waitInput);
if (!(waitInput.find(',') != string::npos)) {
script << "***" << endl;
} else {
script << "***" << waitInput << endl;
}
} else if(input == "action") {
string actionInput;
getline(cin >> ws, actionInput);
int i = actionInput.find(',');
actionInput += ')';
actionInput.insert((i + 2), 1, '(');
script << actionInput << endl;
} else if(input == "stop") {
stopped = true;
}
}
script.close();
return 0;
}

我正在使用我第一次输入"0"的输入;测试";然后点击回车,然后我键入"回车";停止";然后点击回车键,它会生成文件,但在我使用停止部分后,它只有一行空白。

事实证明,这只是因为我用来运行它的atom包并没有因为某种原因而停止它,我尝试使用konsole而不仅仅是atom,它起到了的作用

好吧,由于此代码在我输入stop时停止(我建议在本地尝试确认(,您没有向我们展示的代码中一定有问题。

#include <iostream>
#include <string>
int main() {
bool stopped = false;
while (!stopped) {
std::string input;
std::cin >> input;
if (input == "stop") {
stopped = true;
}
// some more code here?
}
}

我猜循环正在停止,但您的其他代码仍在对您在一条评论中提到的输出文件执行某些操作。例如,如果您有一些代码,我的// some more code here?注释所在,那么即使您输入了stop,它也会执行。实际循环在返回到while位之前不会退出。

在我们看到真正的代码之前,我们没有太多其他事情可做


现在您已经添加了完整的代码,我很有信心问题不在于代码。在我的系统上运行该代码会得到:

pax:~> g++ --std=c++17 -Wall -Wextra -Wpedantic -o prog prog.cpp && ./prog
test
stop
pax:~> wc test
0 0 0 test

它在stop上退出,结果文件中不包含任何内容,正如预期的那样。

现在它可能是由你运行它的方式引起的环境问题。我知道在Visual Studio中运行东西有时会让你打开窗口,但它至少会打印一些东西来通知你:

C:UsersPaxConsoleApp1.exe (process 5400) exited with code 0.      Press any key to close this window . . .

如果是从IDE运行它,我会从适当的shell(cmd.exe或终端(运行它,只是为了检查是否是这种情况。


顺便说一句,我对你的代码做了一些修改,这样你就知道它在每一点上都在做什么(有提示,而不仅仅是带光标的空行(:

#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
using namespace std;
int main() {
bool stopped = false;
ofstream script;
string scriptname;
cout << "scriptname: ";
getline(cin >> ws, scriptname);
script.open(scriptname);
while (!stopped) {
string input;
cout << "input: ";
cin >> input;
if (input == "line") {
string lineInput;
cout << "lineInput: ";
getline(cin >> ws, lineInput);
int i = lineInput.find(',');
lineInput[i] = ':';
script << lineInput << endl;
} else if(input == "wait") {
string waitInput;
cout << "waitInput: ";
getline(cin >> ws, waitInput);
if (!(waitInput.find(',') != string::npos)) {
script << "***" << endl;
} else {
script << "***" << waitInput << endl;
}
} else if(input == "action") {
string actionInput;
cout << "actionInput: ";
getline(cin >> ws, actionInput);
int i = actionInput.find(',');
actionInput += ')';
actionInput.insert((i + 2), 1, '(');
script << actionInput << endl;
} else if(input == "stop") {
stopped = true;
}
}
script.close();
cout << "EXITn";
return 0;
}

这似乎可以很好地处理所有写入文件的内容。然而,至少有一个小编码错误(在多个空间中(:

int i = lineInput.find(',');
lineInput[i] = ':';

如果输入不带逗号的行,则i将设置为std::npos,即-1(或等效的无符号(。如果你在上面的第二行中使用它作为索引,它会将一个字符更改为超过字符串末尾的,或者更改为字符串开头之前的字符。这两种情况都是未定义的行为(这就是为什么我没有花太多时间研究它(。

你可能应该有类似于wait案例中的保护:

int i = lineInput.find(',');
if (i != std::npos) {
lineInput[i] = ':';
}

action的情况下(对于insert调用(,您也需要类似的东西,我会去掉wait:中的双负

// if (!(waitInput.find(',') != string::npos)) {
// Instead, you <humour> shouldn't not </humour> do it this way:
if (waitInput.find(',') == string::npos) {
script << "***" << endl;
} else {
script << "***" << waitInput << endl;
}

最新更新