Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.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只接受数字_C++ - Fatal编程技术网

C++ 如何使cin只接受数字

C++ 如何使cin只接受数字,c++,C++,这是密码 double enter_number() { double number; while(1) { cin>>number; if(cin.fail()) { cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); cout << "Inval

这是密码

double enter_number()
{
  double number;
  while(1)
  {

        cin>>number;
        if(cin.fail())
        {
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Invalid input " << endl;
        }
        else
        break;
        cout<<"Try again"<<endl;
  }
  return number;
}
双输入_编号()
{
双数;
而(1)
{
cin>>数量;
if(cin.fail())
{
cin.clear();
cin.ignore(数值限制::max(),'\n');

cout当cin遇到无法正确读入指定变量的输入(例如将字符输入到整数变量中)时,它将进入错误状态并将输入留在其缓冲区中

要正确处理这种情况,您必须做几件事

  • 您必须测试此错误状态
  • 您必须清除错误状态
  • 您必须或者处理生成错误状态的输入数据,或者将其清除并重新提示用户
  • 下面的代码提供了执行这三件事的众多方法之一

    #include<iostream>
    #include<limits>
    using namespace std;
    int main()
    {
    
        cout << "Enter an int: ";
        int x = 0;
        while(!(cin >> x)){
            cin.clear();
            cin.ignore(numeric_limits<streamsize>::max(), '\n');
            cout << "Invalid input.  Try again: ";
        }
        cout << "You enterd: " << x << endl;        
    }
    
    #包括
    #包括
    使用名称空间std;
    int main()
    {
    cout>x){
    cin.clear();
    cin.ignore(数值限制::max(),'\n');
    
    cout我将使用
    std::getline
    std::string
    读取整行,然后在可以将整行转换为双精度时才中断循环

    #include <string>
    #include <sstream>
    
    int main()
    {
        std::string line;
        double d;
        while (std::getline(std::cin, line))
        {
            std::stringstream ss(line);
            if (ss >> d)
            {
                if (ss.eof())
                {   // Success
                    break;
                }
            }
            std::cout << "Error!" << std::endl;
        }
        std::cout << "Finally: " << d << std::endl;
    }
    
    #包括
    #包括
    int main()
    {
    std::字符串行;
    双d;
    while(std::getline(std::cin,line))
    {
    std::stringstream ss(线路);
    如果(ss>>d)
    {
    if(ss.eof())
    {//成功
    打破
    }
    }
    std::cout
    #包括
    #包括
    #包括
    使用名称空间std;
    int get_int(void);
    int main()
    {
    输入(“输入一个数字”);
    
    是否可以将所有内容转换为字符串并相应地分析输入?使用cin.get()然后检查EOF字符?@Jeff是的,它会刷新缓冲区。当cin将“\n”保留在缓冲区中而不删除输入时,您也可以在使用cin和getline时使用它(我相信,不完全确定,可能是cin.get)因此,GETLIN在它遇到左边的“N”时,它就被限定在一个输入缓冲区中。当用户输入了“代码> 5A < /代码>之类的东西时,它就不能被阻止。在下次尝试输入时,你不会检测到错误。这个代码非常糟糕。它没有任何理由混合C和C++,强加了H。Ad编码的行限制,加上一个空终止符的行不能用多字节换行符,它不能用于输入C和C++中的0,STRL()在C++中存在。它可以用于输入0。你不必尝试它。关于STR [斯特伦(STR)- 1 ]。='\0';我需要它,因为否则str将包含换行符作为最后一个字符,代码将无法工作。如果您认为可以做得更好,请给出更好的解决方案。
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    using namespace std;
    int get_int(void);
    int main()
    {
        puts("Enter a number");
        cout<<"The integer is "<<get_int()<<endl;
        return 0;
    }
    int get_int(void)
    {
        char str[20];
        char* end;
        int num;
        do{
            fgets(str,20,stdin);
            str[strlen(str)-1]='\0';
            num=strtol(str,&end,10);
            if(!(*end))
                return num;
            else
            {
                puts("Please enter a valid integer");
                num=0;
            }
        }while(num==0);
    }