How to compile a function in C ++

I have a problem with C ++ programming language, please help me. I am writing a program that asks a user to enter a student ID, and the program will extract information about a student who has this ID from a list of 15 students in my .txt file. I have these lines of code:

    void Person_list::changeName()
    {
        Person *s;
        string name;
        int id;
        int temp_id;
        s = head;
        cout << "Please enter student ID: ";
        cin >> id;
        while ((s!=NULL) && (s -> getID() != id))
        {
            s = s -> next;
        }
        if (s != NULL)
        {
            s -> Show(); 
        }
        if (s == NULL)
        {
        cout << "Cant find" << endl;
        }
    }

What I want to do is ask the user to enter the student ID again (until the user dials the appropriate number) if the program cannot find the student ID (for example, when I enter 16, the program is not able to find the student, because my list contains only 15 students). Any ideas how to do this? thanks.

P / s I'm not allowed to use nullptr

+4
source share
1

- :

Person* Person_list::FindById(int id)
{
    for (Person* s = head; s != nullptr; s = s->next) {
        if (s->getID() == id) {
            return s;
        }
    }
    return nullptr;
}

void Person_list::changeName()
{
    Person* s = nullptr;
    do {
        std::cout << "Please enter student ID: ";
        int id;
        std::cin >> id;
        s = FindById(id);
        if (s == nullptr) {
            std::cout << "Cant find" << std::endl;
        }
    } while (s == nullptr);
    s->Show();
}
+3

Source: https://habr.com/ru/post/1607297/


All Articles