满足退出条件时程序不退出?
Program not exiting when exit conditions met?
我正在使用 C++ 开发一个 shell 项目。我需要的所有功能都在那里。然而,有一个问题。我的退出条件是当用户输入 "exit" 时,它工作正常,除非前一个命令是随机字符串(例如:asldjkf)。当输入一个随机字符串,然后在下一个提示符下输入 exit 时,它会再次循环 ps,然后在用户再次输入 "exit" 时退出。
我刚刚使用 cout 进行了调试,我知道退出条件已满足。谁能告诉我这里可能发生了什么?`
这是我在 main() 中调用的内容
UNIX_shell myShell;
char * args[100];
string check = "";
argsIndex = 0;
string userIn;
while(myShell.exitstatus == false)
{
std::cin.getline(myShell.userIn, 256);
check = string(myShell.userIn);
cout << check << endl;
myShell.getArgs(check, args);
if(myShell.exitstatus == false && string(myShell.userIn) != "exit")
cout << myShell.userName + "@" + myShell.hostName + ":~" + getcwd(NULL, 0) + "/";
}
return 0;
这里是在调用 getArgs() 后立即满足退出条件的地方:
void UNIX_shell::getArgs(string check, char * args[])
{
cout << "check is" << check << endl;
if(check[0] == 'e' && check[1] == 'x' && check[2] == 'i' && check[3] == 't')
{
cout << "shadoom" << endl;
this->exitstatus = true;
cout << "exit = "<< exitstatus << endl;
return;
}
...
因此,如果我 运行 我的程序并输入类似 ls 或 ps -aef 的内容,然后键入 exit,一切 运行 都很好,程序退出。
但是如果我 运行 我的程序,在命令行中输入 "shit",然后在下一个提示符下输入 "exit",我得到第二个提示符,然后当我输入 exit再次,它退出。我一直在尝试调试这个一个多小时。提前感谢您的帮助。
正如 Mats 所建议的,最好避免使用 char* 并坚持使用字符串。
这是一个示例代码,使用字符串执行与您相同的操作。
以下函数将在每个字符分隔符(例如空格)处拆分一个字符串,并用生成的片段填充元素。我是从这里 .
得到的
std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems)
{
elems.clear();
std::stringstream ss(s);
std::string item;
while(std::getline(ss, item, delim))
elems.push_back(item);
return elems;
}
这将是您的主要代码,循环遍历用户提供的输入字符串。
std::string inputStr;
std::vector<std::string> args;
for(;;)
{
std::getline(std::cin, inputStr);
if(inputStr == "exit")
break;
split(inputStr, ' ', args);
...
}
我正在使用 C++ 开发一个 shell 项目。我需要的所有功能都在那里。然而,有一个问题。我的退出条件是当用户输入 "exit" 时,它工作正常,除非前一个命令是随机字符串(例如:asldjkf)。当输入一个随机字符串,然后在下一个提示符下输入 exit 时,它会再次循环 ps,然后在用户再次输入 "exit" 时退出。
我刚刚使用 cout 进行了调试,我知道退出条件已满足。谁能告诉我这里可能发生了什么?`
这是我在 main() 中调用的内容
UNIX_shell myShell;
char * args[100];
string check = "";
argsIndex = 0;
string userIn;
while(myShell.exitstatus == false)
{
std::cin.getline(myShell.userIn, 256);
check = string(myShell.userIn);
cout << check << endl;
myShell.getArgs(check, args);
if(myShell.exitstatus == false && string(myShell.userIn) != "exit")
cout << myShell.userName + "@" + myShell.hostName + ":~" + getcwd(NULL, 0) + "/";
}
return 0;
这里是在调用 getArgs() 后立即满足退出条件的地方:
void UNIX_shell::getArgs(string check, char * args[])
{
cout << "check is" << check << endl;
if(check[0] == 'e' && check[1] == 'x' && check[2] == 'i' && check[3] == 't')
{
cout << "shadoom" << endl;
this->exitstatus = true;
cout << "exit = "<< exitstatus << endl;
return;
}
...
因此,如果我 运行 我的程序并输入类似 ls 或 ps -aef 的内容,然后键入 exit,一切 运行 都很好,程序退出。
但是如果我 运行 我的程序,在命令行中输入 "shit",然后在下一个提示符下输入 "exit",我得到第二个提示符,然后当我输入 exit再次,它退出。我一直在尝试调试这个一个多小时。提前感谢您的帮助。
正如 Mats 所建议的,最好避免使用 char* 并坚持使用字符串。
这是一个示例代码,使用字符串执行与您相同的操作。
以下函数将在每个字符分隔符(例如空格)处拆分一个字符串,并用生成的片段填充元素。我是从这里 .
得到的std::vector<std::string> &split(const std::string &s, char delim, std::vector<std::string> &elems)
{
elems.clear();
std::stringstream ss(s);
std::string item;
while(std::getline(ss, item, delim))
elems.push_back(item);
return elems;
}
这将是您的主要代码,循环遍历用户提供的输入字符串。
std::string inputStr;
std::vector<std::string> args;
for(;;)
{
std::getline(std::cin, inputStr);
if(inputStr == "exit")
break;
split(inputStr, ' ', args);
...
}