What is the issue with this C++ code? -


i want use algorithm finding student score. want enter number , if number among scores, find name , student number , if not, receive not available.but, there problem cannot find that. have solution? thanks,

#include "stdafx.h" #include <iostream> #include<vector> #include <string> #include <algorithm> #define n 3  using namespace std; class student{ public:     int stno,score,i;     string name; };   int main(){     int l;    vector<student> my_vector;     for(int = 0; < n; i++)     {         student new_student;          cout << "student number: ";         cin >> new_student.stno;         cout << "score: ";         cin >> new_student.score;        cout << "name: ";         cin >> new_student.name;          my_vector.push_back(new_student);     }  cout<<"which score thinking about?="; cin>>l;  (find(my_vector.begin(), my_vector.end(),[] (student const& scores){     if (scores.score ==l)         cout<<"it available"<<scores.name<<scores.stno;     else       cout<<"nit available;}));  cin.get(); cin.get(); } 

you should using std::find_if, not std::find.

you're supposed return true or false depending on whether data has been found or not. returned iterator std::find_if can checked:

auto iter = std::find_if(my_vector.begin(), my_vector.end(),                       [] (student const& scores){ return scores.score ==l; });  if ( iter != my_vector.end())       cout<<"it available"<<(*iter).name<<(*iter).stno; else       cout<<"not available; 

Comments