在字符串比较中检查变量用法

CHECK variable usage in comparing of Strings?

本文关键字:变量 用法 检查 字符串 比较      更新时间:2023-10-16
void display_a_student(char n[])
{
    cout<<"nSTUDENT DETAILSn";
    int check=0; ***** WHY ARE WE USING THIS CHECK ??????
    fp.open("student.dat",ios::in);
    while(fp.read((char*)&st,sizeof(student)))
    {
        if((strcmpi(st.get_ID_number(),n)==0))
        {
            st.show_student();
            check=1; ----> What is this ?
        }
    }
    fp.close();
    if(check==0)
        cout<<"nnStudent does not exist";
    getch();
}

我不理解代码中的这个"检查"??请尽快告诉我答案:) 这是代码。

这是一个标志。它在此处用于检查用户记录是否存在。如果您看到此代码,

if(check==0)
                cout<<"nnStudent does not exist";

你可以看到他们检查它是否为零并打印字符串"Student does not exist",这意味着零是默认值。如果存在学生详细信息,则将其设置为 1。该检查在以下代码中完成,

if((strcmpi(st.get_ID_number(),n)==0)) --> This will be true only if student record exists.
                {
                           st.show_student();
                           check=1; ----> The flag is set here.
                }

最后,这个标志不需要总是命名为"check"。 你可以有自己的名字。

我认为编码人员尝试使用检查变量作为一种机制来判断文件中是否存在任何学生记录。

如果未找到学生记录..,则它将打印消息:cout <<"学生不存在";..