1

I want my array input such that it cannot have the same number twice: this however will have an output of "value exist please re enter: "; two times. how do i check if it is unique and only display once if it has been initialised before?

int main(){
  int arr_size = 10;
  int value;
  int aArray[10];
  for(int i=0;i<arr_size;i++)
  {
        cout<<"enter value of slot"<<i+1<<": ";
        cin>>value;

        for(int j=0;j<arr_size;j++){

          if(value == aArray[j])
          {
            cout<<"value exist please re enter: ";
            cin>>value;
          }
          else{

          aArray[i] = value;
          }
        }
    }

  }
4
  • 3
    Use a std::set<int> instead of the raw integer array. Commented Jul 16, 2014 at 14:04
  • You might consider inserting into a std::set and checking the result of that. Even for a minimal change, use std::find instead of a loop. Also note that you're reading uninitialized data if the element is not found in those set so far. Commented Jul 16, 2014 at 14:04
  • Or you just introduce a break after the value was typed again. But nevertheless you don't initialize your error and therefore your upper limit for the existence check (i.e. the j-loop) should be i not arr_size since in every element greater than i can anything be inside. Commented Jul 16, 2014 at 14:07
  • @isme Another note: you should use a const int arr_size = 10; and then define int aArray[arr_size], so that later on you have to change the size of your array you can do it just in one place, and above all you will avoid forgetting to change in both places (those are errors difficult to detect, as they will show up only during runtime, and sometimes with crazy behavior difficult to bring back to the original error). Commented Jul 17, 2014 at 8:53

1 Answer 1

2

Change to:

  for(int i=0;i<arr_size;i++)
  {
      cout<<"enter value of slot"<<i+1<<": ";
      while(1) { //You must keep reading until you have read a valid value
        cin>>value;
        bool alreadyPresent = false;    

        for(int j=0;j<i;j++){ //You only have to check against already inserted values!
                              //Before you were checking against uninitialized values!!
          if(value == aArray[j])
          {
            alreadyPresent = true;
            break; //I don't need to further iterate the array
          }

        }

        if (alreadyPresent)
          cout<< std::endl << value exists, please re enter: ";
        else
          break; //I can proceed with the next value, user has not to reenter the value
       }
     aArray[i] = value;

     std::cout << std::endl; //next line...
  }

Alternative:

  for(int i=0;i<arr_size;i++)
  {
      cout<<"enter value of slot"<<i+1<<": ";

      bool alreadyPresent;
      do { //You must keep reading until you have read a valid value
        cin>>value;
        alreadyPresent = false;    

        for(int j=0;j<i;j++){ //You only have to check against already inserted values!
                              //Before you were checking against uninitialized values!!
          if(value == aArray[j])
          {
            alreadyPresent = true;
            cout<< std::endl << value exists, please re enter: ";
            break; //I don't need to further iterate the array
          }

        }

      } while (alreadyPresent);
     aArray[i] = value;

     std::cout << std::endl; //next line...
  }
Sign up to request clarification or add additional context in comments.

6 Comments

@Erbureth Yep, definitely you are right, otherwise it would check only the first value
You should drop the second break and change while(1) to while(alreadyPresent) while moving bool alreadyPresent = true to the outer for-loop. Or even change your while-loop in a do ... while-loop while getting value for a first time.
You may use bool alreadyPresent = std::find(aArray, aArray + i, value) != aArray + i; instead of your own loop.
@Jarod42 Yep, but using library functions maybe would not help OP to understand the problems in his code.
@a_guest I added also your solution, it flows better, I agree
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.