如果键入了单词 "exit",如何在命令 promt 中进行此退出?
How do i make this exit in the command promt if the word "exit" is typed?
我必须编写一个程序,要求用户在命令提示符中输入句子。如果用户键入"退出"或"退出"一词(没有引号和所有较低的情况(,则该程序应退出。否则,程序应打印用户键入屏幕的内容,并要求用户键入其他内容。我了解如何获取句子,但我不知道如何使程序退出命令提示符。请帮助?
#include <iostream>
#include <string>
using namespace std;
int main()
{
string data;
cout << "Type a sentence and press enter."
"If the word 'exit' is typed, the program will close." << endl;
getline(cin, data);
cout << data;
return 0;
}
您可以将接收数据与"退出"进行比较。如果您只需显示输入用户数据,请尝试以下操作:
int main() {
string data;
cout << "Type a sentence and press enter."
"If the word 'exit' is typed, the program will close." << endl;
getline(cin, data);
// validate if data is equals to "exit"
if (data.compare("exit") != 0) {
cout << data;
}
return 0;
}
如果要在输入"退出"时输入输入输入,请尝试以下操作:
int main() {
string data;
do {
cout << "Type a sentence and press enter."
"If the word 'exit' is typed, the program will close." << endl;
getline(cin, data);
// validate if data is not equals to "exit"
if (data.compare("exit") != 0) {
// then type back
cout << data << endl;
} else {
// else interrupt while
break;
}
// will run while break or return be called
} while (true);
// terminate the program
return 0;
}
您可以尝试以下代码:
#include <iostream>
#include <cstdlib>
#include <boost/algorithm/string.hpp>
using namespace std;
int main() {
string data;
while(true) {
cout << "Type a sentence and press enter."
"If the word 'exit' is typed, the program will close." << endl;
getline(cin, data);
if ( boost::iequals(data, "exit") )
exit(0);
else
cout << data;
}
}
相关文章:
- 从命令行c++发送文本文件名
- 如何使用 < 和 > 命令获取 c++ 中的输入和输出?
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 使用QProcess执行命令,并将结果存储在QStringList中
- 数组长度,为什么从命令行获取时不能使用它?
- 如果用户输入无效,如何使用字符串变量-C++重复输入命令
- clang整洁10忽略了我的NOLINT命令
- 在子目录中使用target_sources()命令时用于单元测试(qtest)的项目结构
- 如何处理linux终端中带有负号(-)的C++中的命令行参数
- VS Code "command":"make"与终端窗口中的命令行"make"不同
- 使用VS Code和CMake Tools运行自定义命令
- 如何使用ESP8266向谷歌主页发送命令
- 如何使 windows 命令提示符在C++可执行文件上显示返回值?
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 通过 Openssl 命令行加密,通过 c++ 解密
- CMake "--target install"无需"--build"命令行
- Gstreamer 管道从命令 lne 到 c 代码
- 自动"yes"到 Popen SSH 命令
- 如果键入了单词 "exit",如何在命令 promt 中进行此退出?