我如何使用 (!(cin>>a)) 两次?

how can i use (!(cin>>a)) twice times?

enter image description here 我已经使用代码( if (!(cin >> arr[i])) )来检查用户的输入是否与类型 int(如字符串、char)不同以停止读入数组(arr1),然后我可以'在第二个数组 (arr2) 中没有使用它两次,它没有读取输入并直接转到 cin.clear 和 return... 你能帮我吗?非常感谢你^^ enter image description here

看来你的意思如下

#include <limits>

//...

if ( not ( std::cin >> arr[i] ) )
{
    //...
    std::cin.clear();
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
}

以下是两种方式的完整工作示例。给定的程序读取与用户在控制台上输入的整数一样多的整数。例如,用户可以输入 30 或 300 个输入,其中一些可能是整数类型,而另一些可能是其他类型,如字符串。 array/vector 将根据需要在自身内部仅添加整数类型输入。

解决方案 1:使用内置数组

#include <iostream>
#include <sstream>
#include <string>
int main()
{
    
    std::string line;
   //in case of using array, size must be fixed and predetermined 
   int arr[4] = {0}; //all elements are initialzed to zero. Also you can choose array size according to your needs
    
        int i = 0;//this variable will be used to add element into the array
        int count = 0;
        getline(std::cin, line);      
        
            
        std::istringstream s(line);
            
        //take input(from s to i) and then checks stream's eof flag status
        while(s >> i || !s.eof()) {
            //check if either failbit or badbit is set
            if(s.fail()) 
            {
                //clear the error state to allow further operations on s
                s.clear();
                std::string temp;
                s >> temp;
                continue;
             }
            else 
            {
                arr[count] = i;
                ++count;
                if(count>=4)
                {
                    break;//break the loop so that you don't go beyond array size
                }
            }      
        
    }

    
  
    //print out the elements of the array
    for(int i: arr)
    {
        std::cout<<"elem: "<<i<<std::endl;
    }
   
    return 0;
}

解决方案 1 可以检查 here

解决方案 2:使用 std::vector

#include <iostream>
#include <vector>
#include <sstream>
#include <string>
int main()
{
    
    std::string line;
   
    std::vector<int> vec;
    
    int i = 0;//this variable will be used to add element into the array
    
    getline(std::cin, line);      
    
        
    std::istringstream s(line);
        
    //take input(from s to i) and then checks stream's eof flag status
    while(s >> i || !s.eof()) 
    {
        //check if either failbit or badbit is set
        if(s.fail()) 
        {
            //clear the error state to allow further operations on s
            s.clear();
            std::string temp;
            s >> temp;
            continue;
         }
        else 
        {
            vec.push_back(i);
           
        }      
    
    }


  
    //print out the elements of the array
    for(int i: vec)
    {
        std::cout<<"elem: "<<i<<std::endl;
    }
   
    return 0;
}

方案2可以查看here

重要提示

使用 std::vector 优于内置数组(在本例中)的优点是您事先不知道向量的大小。也就是说 std::vector 的大小不是固定的,而不是内置数组。所以它更可取,因为您不知道用户将要输入多少输入。 std::vector 可以相应地处理此问题并仅添加有效(整数类型)输入。但是当使用内置数组时,你必须预先 know/specify 数组的大小。这反过来意味着您必须事先知道用户要输入多少个整数,这是不切实际的。