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;
}
}
}
}
std::set<int>instead of the raw integer array.std::setand checking the result of that. Even for a minimal change, usestd::findinstead of a loop. Also note that you're reading uninitialized data if the element is not found in those set so far.breakafter the value was typed again. But nevertheless you don't initialize your error and therefore your upper limit for the existence check (i.e. thej-loop) should beinotarr_sizesince in every element greater thanican anything be inside.const int arr_size = 10;and then defineint 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).