Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何使用cin.get()检测空用户输入?_C++_Input - Fatal编程技术网

C++ 如何使用cin.get()检测空用户输入?

C++ 如何使用cin.get()检测空用户输入?,c++,input,C++,Input,我编写了一些代码来检测空用户输入: if(cin.get() == '\n') { cout<<"ENTER WAS PRESSED"<<endl; } if(cin.get()='\n') { 在这种形式下,您的代码对我有效: #include <iostream> int main() { while (true) { if (std::cin.get() == '\n') { std::cout&

我编写了一些代码来检测空用户输入:

if(cin.get() == '\n')
{
   cout<<"ENTER WAS PRESSED"<<endl;
}
if(cin.get()='\n')
{

在这种形式下,您的代码对我有效:

#include <iostream>

int main() {
    while (true) {
        if (std::cin.get() == '\n') {
            std::cout<<"ENTER WAS PRESSED"<<std::endl;
        }
    }
    return 0;
}
#包括
int main(){
while(true){
如果(std::cin.get()='\n'){

std::cout在这种形式下,您的代码适用于我:

#include <iostream>

int main() {
    while (true) {
        if (std::cin.get() == '\n') {
            std::cout<<"ENTER WAS PRESSED"<<std::endl;
        }
    }
    return 0;
}
#包括
int main(){
while(true){
如果(std::cin.get()='\n'){

我想这段代码可以胜任你的工作

#include <iostream>
#include <sstream>

using std::cin;
using std::cout;
using std::string;

int main()
{
    string in = "";
    int n = 0;
    while (true)
    {
        cout << "Please enter a number: ";
        getline(cin, in);
        std::stringstream s(in);
        if (s >> n)
            break;
        cout << "Invalid number, please try again\n";
     }
     return 0;
}
#包括
#包括
使用std::cin;
使用std::cout;
使用std::string;
int main()
{
在“”中输入字符串;
int n=0;
while(true)
{
cout>n)
打破

我想这个代码可以胜任你的工作

#include <iostream>
#include <sstream>

using std::cin;
using std::cout;
using std::string;

int main()
{
    string in = "";
    int n = 0;
    while (true)
    {
        cout << "Please enter a number: ";
        getline(cin, in);
        std::stringstream s(in);
        if (s >> n)
            break;
        cout << "Invalid number, please try again\n";
     }
     return 0;
}
#包括
#包括
使用std::cin;
使用std::cout;
使用std::string;
int main()
{
在“”中输入字符串;
int n=0;
while(true)
{
cout>n)
打破

我想您必须使用noskipws或类似的东西。默认情况下,cin跳过空格,\n是空格IIRC。很抱歉,我忘了提到用户应该输入一个数字,我想知道该怎么做,以确保如果用户按enter键,则会弹出警告消息,并不断询问,直到您得到所需的内容“Whitespace”和“代码> /N/CODE”是不同的实体。它们被编译器处理的不同。而且,空白的ASCII值是32,而 > \N>代码>是10。@ CuthToStION: C++编译器将代码< > \N/COD>作为空白,讨论这里所讨论的所有上下文。我认为您必须使用NoSkIPWS或A隆。g这些行。默认情况下,cin跳过空白,并且\n是空白IIRC。很抱歉,我忘了提到用户应该输入一个数字,我想知道该怎么做,以确保如果用户按enter键,则会弹出警告消息,不断询问,直到您得到所需的内容为止?@Borgleader:whitespace和
/n
是d它们的处理方式不同,编译器的处理方式也不同。空白空间的ASCII值为32,而<>代码> \n>代码>为10。@ CuthToStION: C++编译器将代码< > \N/COD>作为空白,讨论这里所讨论的所有上下文。还有NeWB:为什么你不应该使用“命名空间STD”?@博比:因为“使用命名空间STD”;转储标准命名空间。它禁用了C++相对于标准库符号的命名空间机制。假设你定义了一个函数<代码> BAIR()/代码>。假设你无意中把它称为“代码”BAZ。()
。没问题,对吗?编译器会为您标记此项。但是,假设标准库提供了一个
std::baz()
。在“使用命名空间std;”之后会发生什么?最好是“使用std::baz;”而不是抛弃几百个程序员不知道的标准库符号,只导入一个所需的符号。@ Bobby:C++是一种商业成功,在被采纳为一种教学语言之前,不幸的是,在美国学术市场上,第一个广泛使用的教科书之一。(我不会在这里给它命名的尴尬)它仍然被广泛使用,被一个不知道C++的作者偶然引入到现有C++ C++教材中,作者将其转储到命名空间中。{(数<0×3>3)〕(CIN,GET())='\n′):这里NeWB:为什么不使用“命名空间STD”?@博比:因为“使用命名空间STD”;转储标准命名空间。它禁用C++的标准空间符号的命名空间机制。假设,例如,你已经定义了一个函数<代码> BAL()
。假设您后来无意中将其称为
baz()
。没问题,对吗?编译器会为您标记它。但是,假设标准库提供了一个
std::baz()
。在“使用命名空间std;”之后会发生什么?最好是“使用std::baz;”而不是抛弃几百个程序员不知道的标准库符号,只导入一个所需的符号。@ Bobby:C++是一种商业成功,在被采纳为一种教学语言之前,不幸的是,在美国学术市场上,第一个广泛使用的教科书之一。(我不会在这里给它命名的尴尬)它仍然被广泛使用,被一个不真正了解C++的作者偶然引入到现有的C++ C++教科书中,作者让学生转储了命名空间。