Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/147.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++ I';我正在使用Atoi处理If语句_C++_If Statement_Atoi - Fatal编程技术网

C++ I';我正在使用Atoi处理If语句

C++ I';我正在使用Atoi处理If语句,c++,if-statement,atoi,C++,If Statement,Atoi,我正在尝试制作一个票证客户代码,目前正在显示我的“客户”。我希望它是这样的,如果我输入“blank,like nothing”,然后输入“我希望我自己的DTA文件中的所有客户都在我的输出上。换言之,显示以查看哪些客户已注册 void Customer::DisplayCustomer() { cin.getline(buffer, MAXTEXT) buffernr = atoi(buffer) //I need to convert text to numbers.

我正在尝试制作一个票证客户代码,目前正在显示我的“客户”。我希望它是这样的,如果我输入“blank,like nothing”,然后输入“我希望我自己的DTA文件中的所有客户都在我的输出上。换言之,显示以查看哪些客户已注册

void Customer::DisplayCustomer() {
    cin.getline(buffer, MAXTEXT)
    buffernr = atoi(buffer)   //I need to convert text to numbers. 

    if (buffer[0]=='A' && buffer[1] == '\0')
    // (Here I have a function which displays everything) don't focus on this one
}

我要问的是,为了让我的代码理解,我想为那些在没有输入任何内容的情况下按Enter键的人提供一个if语句,我的display customer函数将运行。我也尝试过(如果缓冲区[0]='\n'),但也不起作用

此代码检查缓冲区是否为空

#include <iostream>

int MAXTEXT{300};

int main() {
    char buffer[MAXTEXT];
    std::cin.getline(buffer, MAXTEXT);
    if (buffer[0] == '\0') {
        std::cout << "Empty" << std::endl;
    }
    return 0;
}

对于您的用例,您似乎希望使用
std::getline()
而不是
std::istream::getline()

void Customer::DisplayCustomer() {
    std::string buffer;
    std::getline(std:cin,buffer);
    std::istringstream iss(buffer);
    int buffernr;
    if(!(iss >> buffernr)) { // This condition would be false if there's no numeric value
                             // has been entered from the standard input
                             // including a simple ENTER (i.e. buffer was empty)
         // (Here i have a function which displays everything) don't focus on this 
         //  one
    }
    else {
        // Use buffernr as it was parsed correctly from input
    }
}

您通常会使用类似于
std::getline(std::cin,buffer)
的东西;这与
atoi
有什么关系?您需要
buffer[0]='\0'
检查缓冲区是否为空如果您按Enter键(没有输入其他数据),您将有一个空缓冲区,因此如果您先检查此条件,则无需调用
atoi
。如果您有
std::string
对象,为什么要费心看NUL终结者?好的,非常感谢你给我看。这就是我需要的答案。我会把这个标记为答案。我不是很熟练的程序员,我只知道这种方法。你认为使用Atoi没有意义吗?@Lillie我不知道你在用
buffernr
做什么,所以我不能说。你以后会在什么地方用吗?是的,我用buffernr来通过他们的号码找到我的客户。每个客户都有自己的ID。这不是我的问题,但我很感激你告诉我这个。我的代码正在运行:)什么是Iss?以前从未见过。@Lillie“什么是Iss?”这是一个例子。很高兴能教你一些新东西。@Lillie我在我的代码提案中添加了一些注释来澄清。您应该考虑使用此作为更一般的解决方案。根本不需要
atoi()
,非常感谢,我将尝试使我的代码像这样。这是非常值得赞赏的。
void Customer::DisplayCustomer() {
    std::string buffer;
    std::getline(std:cin,buffer);
    std::istringstream iss(buffer);
    int buffernr;
    if(!(iss >> buffernr)) { // This condition would be false if there's no numeric value
                             // has been entered from the standard input
                             // including a simple ENTER (i.e. buffer was empty)
         // (Here i have a function which displays everything) don't focus on this 
         //  one
    }
    else {
        // Use buffernr as it was parsed correctly from input
    }
}