# Any help with the problem below?

This code needs to print the position of the number(-99), if the number is in the array. And if is not in the array it needs to print the array's size. Currently the program prints the position of the number as well as the size of the array.

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){

vector<int>vec{9,6,5,4,-3,-32,-99,-45,-55,-77};
int count{};
bool in_the_vector{false};

while (count <= int(vec.size())) {
count ++;

if (vec[count] == -99)

cout<<count<<endl;
in_the_vector == true;

}

if(!in_the_vector){
// (vec[count] != -99);
cout<<int (vec.size())<<endl;
count == int(vec.size());

}
return 0;
}

answered Feb 12 by (7,630 points)
selected Feb 12 by Nedelea Claudiu

Have you tried to debug the code, there are plenty of debuggers present (one present here actually) which can help you correct the code.

Lines # 15-20 have the bug. Please check how if conditions work without the braces & also check what the difference between = & == operator are.

I see you have used the algorithm header , so I would suggest using the find_if algo , it does the same job ,although without having to worry about minor bugs , writing manual loops, etc.

my implementation below:

```#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
using namespace std;

int main(){

vector<int>vec{9,6,5,4,-3,-32,-99,-45,-55,-77};
int count{};
bool in_the_vector{false};

#if 0
//Corrected ur code below,to compile it ,just switch #if 0 to #if 1
while (count <= int(vec.size())) {
count ++;

if (vec[count] == -99){
cout<<count<<endl;
in_the_vector = true;
break;
}
}
if(!in_the_vector){
// (vec[count] != -99);
cout<<int (vec.size())<<endl;
count == int(vec.size());
}
#endif
//My implementation below
auto it = std::find_if (begin(vec), end(vec) , [](int i) { return i==-99 ; });
//we got the iterator of the found element
//lets see if the index is valid
auto index = std::distance(begin(vec) , it);
std::cout << index <<std::endl;
return 0;
}
```
commented Feb 12 by (200 points)
answered Feb 16 by (230 points)

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){

vector<int>vec{9,6,5,4,-3,-32,-99,-45,-55,-77};
int count{};
bool in_the_vector{false};

for(count=0;count <= int(vec.size());count++)
{

if (vec[count] == -99)

cout<<count+1<<endl;
//because count starts from 0 so we have to add 1 in count to get index no.

in_the_vector == true;

}

if(!in_the_vector)
{
// (vec[count] != -99);
cout<<int (vec.size())<<endl;
count == int(vec.size());

}
return 0;
}