什么类型的情况会导致cin.get()函数不起作用? 我对C++非常陌生,我想知道为什么我的CIN。()不停止CMD中的程序在完成时立即关闭?我在以前的代码中尝试了cin.get(),它成功了,但由于某些原因,它在这段代码中不起作用 #include <iostream> int main() { using namespace std; int carrots; cout << "How many carrots do you have?" << endl; cin >> carrots; cout << "You have " << carrots << endl; cin.get(); return 0; } #包括 int main() { 使用名称空间std; 胡萝卜; 胡萝卜; 如果使用cin.get(),则只能获得其中一个字符,并将其视为char。 您将无法看到输出,因为命令提示符将在程序完成后立即关闭。Puttingcin.get()强制程序等待用户输入键后才能关闭,您现在可以看到程序的输出 using namespace std; int carrots; cout << "How many carrots do you have?" << endl; cin >> carrots; cout << "You have " << carrots << endl; cin.get(); cin.get();// add another one return 0; 使用名称空间std; 胡萝卜; 胡萝卜; 如果使用cin.get(),则只能获得其中一个字符,并将其视为char。 您将无法看到输出,因为命令提示符将在程序完成后立即关闭。Puttingcin.get()强制程序等待用户输入键后才能关闭,您现在可以看到程序的输出 using namespace std; int carrots; cout << "How many carrots do you have?" << endl; cin >> carrots; cout << "You have " << carrots << endl; cin.get(); cin.get();// add another one return 0; 使用名称空间std; 胡萝卜; 胡萝卜; 难道你必须加上 cin.ignore();

什么类型的情况会导致cin.get()函数不起作用? 我对C++非常陌生,我想知道为什么我的CIN。()不停止CMD中的程序在完成时立即关闭?我在以前的代码中尝试了cin.get(),它成功了,但由于某些原因,它在这段代码中不起作用 #include <iostream> int main() { using namespace std; int carrots; cout << "How many carrots do you have?" << endl; cin >> carrots; cout << "You have " << carrots << endl; cin.get(); return 0; } #包括 int main() { 使用名称空间std; 胡萝卜; 胡萝卜; 如果使用cin.get(),则只能获得其中一个字符,并将其视为char。 您将无法看到输出,因为命令提示符将在程序完成后立即关闭。Puttingcin.get()强制程序等待用户输入键后才能关闭,您现在可以看到程序的输出 using namespace std; int carrots; cout << "How many carrots do you have?" << endl; cin >> carrots; cout << "You have " << carrots << endl; cin.get(); cin.get();// add another one return 0; 使用名称空间std; 胡萝卜; 胡萝卜; 如果使用cin.get(),则只能获得其中一个字符,并将其视为char。 您将无法看到输出,因为命令提示符将在程序完成后立即关闭。Puttingcin.get()强制程序等待用户输入键后才能关闭,您现在可以看到程序的输出 using namespace std; int carrots; cout << "How many carrots do you have?" << endl; cin >> carrots; cout << "You have " << carrots << endl; cin.get(); cin.get();// add another one return 0; 使用名称空间std; 胡萝卜; 胡萝卜; 难道你必须加上 cin.ignore();,c++,visual-c++,C++,Visual C++,之前 cin.get(); 从先前的返回中清除输入缓冲区 完整代码 #include <iostream> int main() { using namespace std; int carrots; cout << "How many carrots do you have?" << endl; cin >> carrots; cout << "You have " << car

之前

cin.get();
从先前的返回中清除输入缓冲区

完整代码

#include <iostream>

int main()
{
    using namespace std;
    int carrots;
    cout << "How many carrots do you have?" << endl;
    cin >> carrots;
    cout << "You have " << carrots << endl;
    cin.ignore();
    cin.get();
    return 0;
}
#包括
int main()
{
使用名称空间std;
胡萝卜;
胡萝卜;
难道你必须加上

cin.ignore();
之前

cin.get();
从先前的返回中清除输入缓冲区

完整代码

#include <iostream>

int main()
{
    using namespace std;
    int carrots;
    cout << "How many carrots do you have?" << endl;
    cin >> carrots;
    cout << "You have " << carrots << endl;
    cin.ignore();
    cin.get();
    return 0;
}
#包括
int main()
{
使用名称空间std;
胡萝卜;
胡萝卜;

输入缓冲区中是否仍有输入。
get
只读取一个字符。我发现,我需要添加另一个cin.get(),因为当我请求输入时第一次暂停,然后需要第二次。但是,输入缓冲区是什么意思?我刚刚编写了一个名为
holdScreen()的函数
其中我使用
getline()
等待回车键。s@user3720526,输入保存在缓冲区中,提取函数从缓冲区中提取。它们通常不会读取所需的内容。如果输入一个
int
,然后键入“23 34”,“34”仍然保留在那里。输入缓冲区中仍然有输入。
get
只读取一个字符。我发现,我需要添加另一个cin.get(),因为当我请求输入时第一次暂停,然后需要第二次。但是,输入缓冲区是什么意思?我刚刚编写了一个名为
holdScreen()的函数
其中我使用
getline()
等待回车键。s@user3720526,输入保存在缓冲区中,提取函数从缓冲区中提取。它们通常不会读取所需的内容。如果您输入一个
int
,然后键入“23 34”,则仍会保留34。