0

I hope you all have had a fantastic day!

My problem is that I do not know how to search after a name of an object (if that even is the right way to put it). But an example is: I have made a student class containing first names, last names, grade and year of birth. One of the students name could be "Isaac". What I want to do is write in the "Command Prompt" a name and the result should be the info of the student name I wrote. If the name is not listed as one of the students, I should be able to try again.

Down here we have my small code. In "search_after_student_name" should be the place where the computer searches through the first_name's of the students to see the info of that student.

#include <iostream>
#include <Windows.h>
#include <cstring>
using namespace std;

class Student {
public:
    string first_name;
    string last_name;
    char grade;
    int birth;
    Student(string aFirst_name, string aLast_name, char aGrade, int aBirth) {
        first_name = aFirst_name;
        last_name = aLast_name;
        grade = aGrade;
        birth = aBirth;
    }
};

void search_after_student_name(string hisFirst_name)
{
    
    if (hisFirst_name != student.first_name) //Is not like that, but just an example of how I am going to use it
        cout << "We couldn't find the name you wrote. Try again" << endl;
    else() //if the "hisFirst_name" matches a student:
    {
        cout << "info about this student...";
    }
}

int main()
{
    SetConsoleCP(1252);
    SetConsoleOutputCP(1252);

    Student student1("Oliver", "Twist", 'C', 1999);
    Student student2("James", "Free", 'D', 2000);
    Student student3("Isaac", "Lee", 'A', 2000);

    string namn;
    cout << "Write in the name that you want to search: ";
    cin >> namn;

    search_after_student_name(namn);
    return 0;
} 

I hope you understand my problem with my silly code and help me. Thanks!

  • 4
    The way this code is written you can't search for anything. The scope of your objects is `main`, your function has no way to access those objects. And you should really store them in some kind of data structure (e.g.: a `std::vector` or `std::map`) if you intend to do searches – UnholySheep Jul 15 '21 at 13:55
  • 1
    You need create an array or vector of students and pass that to you search function along with the name to search for. Then you can use [this](https://stackoverflow.com/questions/15517991/search-a-vector-of-objects-by-object-attribute) to find the object. – NathanOliver Jul 15 '21 at 13:55
  • I would suggest the OP to get his/her OOP concepts cleared. Who keeps the data members public? – Abhishek Dutt Jul 15 '21 at 14:00
  • @ILoveLogCat It was just a little task I wanted to do, nothing more than that. – lazycat00 Jul 15 '21 at 14:01
  • Does this answer your question? [How to use std::find/std::find\_if with a vector of custom class objects?](https://stackoverflow.com/questions/6939129/how-to-use-stdfind-stdfind-if-with-a-vector-of-custom-class-objects) – pptaszni Jul 15 '21 at 14:38

2 Answers2

1

As pointed out in comments you should use some data structure to hold your data. This could be a solution:

#include <iostream>
#include <Windows.h>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

class Student
{
public:
    string first_name;
    string last_name;
    char grade;
    int birth;
    Student(string aFirst_name, string aLast_name, char aGrade, int aBirth)
    {
        first_name = aFirst_name;
        last_name = aLast_name;
        grade = aGrade;
        birth = aBirth;
    }
};

void search_after_student_name(const string& hisFirst_name, const std::vector<Student>& students)
{
    auto studentIt = std::find_if(std::begin(students), std::end(students), [&hisFirst_name](const auto & s)
    {
        return s.first_name == hisFirst_name;
    });

    if (studentIt == std::end(students)) 
        cout << "We couldn't find the name you wrote. Try again" << endl;
    else //if the "hisFirst_name" matches a student:
    {
        cout << "info about this student: ";
        cout << studentIt->first_name << ", "
             << studentIt->last_name << ", "
             << studentIt->grade << ", "
             << studentIt->birth;
    }
}

int main()
{
    SetConsoleCP(1252);
    SetConsoleOutputCP(1252);

    std::vector<Student> students =
    {
        Student("Oliver", "Twist", 'C', 1999),
        Student("James", "Free", 'D', 2000),
        Student("Isaac", "Lee", 'A', 2000),
    };

    string namn;
    std::cout << "Write in the name that you want to search: ";
    std::cin >> namn;

    search_after_student_name(namn, students);
    return 0;
}

Note that i changed search_after_student_name to get data by const reference, if you don't need to change the data this is a better way to pass data to your functions.

Dundo
  • 714
  • 8
  • 12
0

Note that collection of data should be stored in array or other type of containers. Also to keep code clean standard algorithms should be used here, to practice C++20 a bit, std::ranges::find has been used (I also clean up boiler plate a bit):

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ranges>

class Student
{
public:
    std::string first_name;
    std::string last_name;
    char grade;
    int birth;
};

void printStudent(const Student& x)
{
    std::cout << "first_name: " << x.first_name << '\n';
    std::cout << " last_name: " << x.last_name << '\n';
    std::cout << "     grade: " << x.grade << '\n';
    std::cout << "     birth: " << x.birth << '\n';
}

int main()
{
    std::vector students {
        Student{"Oliver", "Twist", 'C', 1999},
        {"James", "Free", 'D', 2000},
        {"Isaac", "Lee", 'A', 2000},
    };

    std::string name;
    std::cout << "Write in the name that you want to search: ";
    std::cin >> name;

    auto it = std::ranges::find(students, name, &Student::first_name);
    if (it == students.end()) {
        std::cout << "\nNot found\n";
    } else {
        std::cout << "\nStudent found:\n";
        printStudent(*it);
    }
    return 0;
}

https://godbolt.org/z/GdafW9Yjv

Marek R
  • 32,568
  • 6
  • 55
  • 140