gpt4 book ai didi

c++ - 在 C++ 中使用 getline 使代码无限运行

转载 作者:行者123 更新时间:2023-11-28 02:43:35 25 4
gpt4 key购买 nike

#include <iostream>
#include <string>

using namespace std;

int main(void) {
string secret = "abracadabra";
string password;
cout << "Enter password:" << endl;
getline(cin,password);
if(secret == password)
cout << "Access granted" << endl;
else
cout << "Sorry";
return 0;
}

我尝试运行上面的程序,但在输入 abracadabra 并按回车键后程序继续运行,

我发现有问题,这次我使用了不同的 ide eclipse,它工作正常。

为什么上面的程序在 eclipse 中运行,而在 netbeans 中运行不正常?

最佳答案

两个 IDE 在开始运行程序之前都必须连接 cin 和 cout 的文件流。显然 netbeans 的做法与 eclipse 不同。

要查看自然行为,请编译您的程序并从命令提示符启动它。

我没有足够的声誉来添加评论,但@AMDG 的回答假定您的密码中不能有空格或制表符。

关于c++ - 在 C++ 中使用 getline 使代码无限运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25120681/

25 4 0
文章推荐: c++ - 使用 QWebPage 加载页面
文章推荐: c++ - 如何使用 CMake 订购/设计使用更高的共享库包括
文章推荐: c++ - 在 C++ 中重载运算符 <<
文章推荐: html - 如何使用 ng-option 在