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

Problems with std::cin and isdigit in c++

当我 运行 代码并在终端中输入例如 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;
}

这个函数是为角色定义的,如果你写isdigit('1').

就可以了

isdigit(49) = true,因为在ascii中49是数字1,所以isdigit(49) = true;

检查 isdigit() 的引用:http://www.cplusplus.com/reference/cctype/isdigit/