samani
samani

Reputation: 3

What is the issue with this C++ code?

I want to use algorithm for finding a student score. I want to enter a number and if this number is among scores, then find the name and student number and if not, I receive it is not available.But, there is a problem that I cannot find that. Do you have any 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 i = 0; i < 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 are you thinking about?=";
cin>>l;

(find(my_vector.begin(), my_vector.end(),[] (student const& scores){
    if (scores.score ==l)
        cout<<"it is available"<<scores.name<<scores.stno;
    else
      cout<<"nit available;}));

cin.get();
cin.get();
}

Upvotes: 0

Views: 66

Answers (1)

PaulMcKenzie
PaulMcKenzie

Reputation: 35440

You should be using std::find_if, not std::find.

You're supposed to return true or false depending on whether the data has been found or not. Then the returned iterator from std::find_if can be 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 is available"<<(*iter).name<<(*iter).stno;
else
      cout<<"not available;

Upvotes: 1

Related Questions