C ++中std :: cin和isdigit的问题

伊德·埃纳伊(Eod Enaj)

当我运行代码并在终端中输入实例1时,它进入“ else”状态并中断。但是我给它提供了一个数字,所以我很难理解它为什么会这样。有人可以帮忙澄清一下吗?

int main() 
{
  vector<int> positions;
  int p;
  
  for(int i = 0; i <= 3; i++){
    
    cout << "Enter a number: ";
    cin >> p;
    
    if(isdigit(p)){
      positions.push_back(p);
    } else
    {
      cout << "Please provide numbers from 0 to 100!" <<"\n";
      break;
    }
    
  }
  return 0;
}
德维·科西塔什维利(Devi khositashvili)

该函数是针对字符定义的,如果您编写 isdigit('1').

另外isdigit(49) = true,由于ascii 49中的数字为1,所以isdigit(49) = true;

检查isdigit()以下网址的参考http : //www.cplusplus.com/reference/cctype/isdigit/

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

使用c + = expression和c = c + expression时输出中的std :: string差异

GCC和Clang无法在C ++ 17中编译std :: hash <std :: nullptr_t>

C#中Char.IsDigit()和Char.IsNumber()之间的区别

为什么C ++ 11中的std :: initializer_list重载了std :: begin()和std :: end()?

C ++中的std :: promise和std :: future

在FreePascal中相当于C ++ std :: vector,std :: deque和std :: map

C ++中std :: fstream :: X和std :: ios :: X之间的区别

在C ++中exit和std :: exit有什么区别?

std :: in_place_t和C ++ 17中的朋友

C ++ 1z中std :: make_pair和std :: make_tuple的用处

C ++ 1z中std :: make_unique和std :: make_shared的用处

OCaml是否具有C / C ++中的isdigit和isalpha这样的模块?

C ++中std :: vector的内存问题

if条件中的str.isdigit()问题

用户定义的类型和C ++中的std :: vector

尝试使用std :: functional和std :: bind在C ++中创建C#样式事件访问修饰符

在VSCode中调试C ++期间将输入传递给std :: cin的问题

如何在C ++中填充和取消填充std :: string?

在C ++中重写<<运算符时如何正确遵守std :: setw和std :: fill

无法在C ++ 20中将std :: cin与char *或char []一起使用

std :: cin.fail()问题

我应该如何使用c ++处理“ std :: cerr”和“ std :: cin.fail()”?

在C ++中使用cin的问题

C ++中的Getline和cin.ignore

c isalpha和isdigit while循环

C++中std::transform的使用问题

C++ 中的 std::ws 与 std::skipws

C++ 中的 std::cin 内部会发生什么?

理解 C++23 中的 std::inout_ptr 和 std::out_ptr