我正在使用 Atoi 的 If 语句苦苦挣扎
I'm struggling with my If-statement using Atoi
我正在尝试制作工单客户代码,我目前正在显示我的"客户"。我希望它像,如果我输入"空白,像什么都没有然后输入",我希望我自己的 DTA 文件中的所有客户都在我的输出中。换句话说,显示,以查看哪些客户已注册。
void Customer::DisplayCustomer() {
cin.getline(buffer, MAXTEXT)
buffernr = atoi(buffer) //I need to convert text to numbers.
if (buffer[0]=='A' && buffer[1] == ' ')
// (Here I have a function which displays everything) don't focus on this one
}
我要问的是,我必须键入什么,以便我的代码理解我希望为按 Enter 而不键入任何内容的人提供一个 if 语句,我的显示客户函数将运行。我也试过(如果缓冲区[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
}
}
此代码检查缓冲区是否为空
#include <iostream>
int MAXTEXT{300};
int main() {
char buffer[MAXTEXT];
std::cin.getline(buffer, MAXTEXT);
if (buffer[0] == ' ') {
std::cout << "Empty" << std::endl;
}
return 0;
}
更好的std::string
解决方案是
#include <string>
#include <iostream>
int main() {
std::string buffer;
std::getline(std::cin, buffer);
if (buffer.empty()) {
std::cout << "Empty" << std::endl;
}
return 0;
}
相关文章:
- Insert函数不适用于2 if语句C++
- If语句未被求值C++
- C++嵌套if语句,基本货币交换
- 多个If语句与使用逻辑运算符计算条件的单个语句的比较
- If语句在c++中被忽略
- 比较if语句中的数组值和int值
- 以在Qt中的IF语句中设置时间延迟
- Craps游戏问题,忽略if语句
- c++:定义if语句中的模板
- C++嵌套的 If 语句中,它无法按预期工作
- 如果条件不相关,我应该更喜欢两个 if 语句而不是 if-else 语句吗?
- 需要有关此 if 语句的帮助
- 转换器练习:跳过 if 语句和 if 语句
- 避免在 C++ 中的 if 语句中递增
- 如果仍然不满足要求,如何使 if 语句重复?
- do while 循环中的 if 语句以 yes 或 no 结尾
- 为什么在此C++代码中触发此 if 语句?
- 如何基于平台在制作文件中制作if语句?
- 在 if 语句中使用 GetKeyState 时,它会跳过条件
- C++ If 语句,从其他语句重复