find values in user entered array

前端 未结 3 920
生来不讨喜
生来不讨喜 2021-01-28 22:53

i am trying to find any user entered value in a array which the user has previously entered the values of. I made the following for finding which values were entered in array bu

3条回答
  •  暖寄归人
    2021-01-28 23:16

    Your problem is that an array of integers has no clear definition between assigned and unassigned. You need a way to "flag" these integers. You could do this with a parity array of booleans, that would be flagged based on std::cin.fail(), or use int* and use NULL to flag. There's a lot of ways to do this.

    std::vector a(10);
    for(int i=0;i<10;i++){
      int input;
      cout<<"enter value : ";
      cin >> input;
      if(!cin.fail()) {
        a[i] = new int(input);
      }else{
        cin.clear(std::ios_base::failbit);
      }
    }
    

    Now you're able to test the vector a for NULL pointers.

提交回复
热议问题