So, a method would be to cout<<"";
if the condition is false but I don't feel like it's the right way and break
doesn't seem to work. Is there something I should know ?
template <class T>
T get(const string &prompt) {
cout<<prompt;
T ret;
cin>>ret;
return ret;
}
int main()
{
vector<int>x;
for(int i=0;i<1000;i++) x.push_back((rand()%1200+1200));
int rating=get<int>("Your rating: ");x.push_back(rating);
sortVector(x);
for(int i=0;i<x.size();i++) ((x[i]==rating) ? cout<<"Found your rating on pozition "<<i : cout<<"");
}
Each part of the ternary operator must be able to evaluate to the same type*, so you can't have one part output (which will return the cout
object) while the other break
's.
Instead, in your situation, why don't you add the condition to your for?
for(int i = 0; i < x.size() && x[i] == rating; i++) {
cout<<"Found your rating on pozition ";
}
It seems you're trying to find an item's position in the array. If this is the case, and you're only looking for the first occurence, I'd suggest this instead:
int pos;
for(pos = 0; pos < x.size() && x[pos] != rating; pos++) { }
if(pos != x.size()) {
cout<<"Found your rating on pozition " << pos;
} else {
cout << "Couldn't find it!";
}
Or even better, use std::find
!
*You could also have a throw in there. Thanks to @Lightness!