按字母顺序对结构内数组变量中的名称进行排序。

问题描述 投票:0回答:1

我在做我的教授要求我们做的这个学生记录程序。它一直说 cannot convert 'StudRec' to 'StudRec' in assignment 有时它说 cannot convert char* to const char*. StudRec 是我的结构变量,而这个函数应该将记录的名字按字母顺序排序。

void sort_name(StudRec name[], StudRec temp[], int studcount)
{
    if (studcount > 0)
    {
        for (int i=0; i<studcount; i++)
        {
           for (int j=studcount; j<=i; j++)
            {
                if(strcmp(name[j].nm, name[j+1].nm) > 0)
                {
                        temp=name[j];
                        name[j]= name[j+1];
                        name[j+1]= temp;
                }
            }
        }
        cout << "\t| |\t\t\t The records have been sorted alphabetically by name.\n";
    }

    else
    {
        cout << "\t| |\t\t\t There is no current record to sort by name.\n\n";
    }
}
c++ arrays sorting structure
1个回答
1
投票

好吧,假设 StudRec 拥有所有必要的操作(赋值、默认构造函数等),你不需要传递一个临时值的数组。

void sort_name(StudRec name[], int studcount)
{
    StudRec temp;
    // ...
}

这应该可以解决一个问题: 你正试图将一个元素分配给整个数组。

        temp=name[j];

更好的办法是定义 temp 就在你使用它的地方。

        const StudRec temp = name[j];

总之,我猜你是想实现一个BubbleSort 而你的实现是不正确的,因为你的索引。应该是这样的。

    for (int i = 1; i < studcount; ++i)
        for (int j = 0; j < studcount - i; ++j)

0
投票

这是fGrade函数的代码:

    void fGrade(StudRec rec[], int studcount)
    {
    string id;

if (studcount > 0)
{
    cout << "\n\n\t| |\t\t         =====================================================\n";
    cout << "\t| |\t\t\t\t         STUDENT'S FINAL GRADE \n";
    cout << "\t| |\t\t         =====================================================\n\n";;
    check:  cout << "\n\t| |\t\t\t Enter student's ID Number: ";
            cin >> id;

            int index = search(rec, id, studcount);

            if (index != -1 && studcount > 0)
            {
                rec[index].fgrd = (((rec[index].quiz / 150) * 100) * 0.2) + (((rec[index].hw / 20) * 100) * 0.1) + (((rec[index].midT / 100) * 100) * 0.15)
        + (((rec[index].fExam / 100) * 100) * 0.15) + (((rec[index].acts / 150) * 100) * 0.25) + (((rec[index].proj / 100) * 100) * 0.15);

        cout << left << setw(10) << "\n ID No." << setw(30) << "NAME" << setw(15) << "FINAL GRADE" << setw(10) << "REMARKS";
        cout << "\n ------------------------------------------------------------\n";
        cout << " " << left << setw(8) << rec[index].id << setw(30) << rec[index].nm << setw(15) << fixed << setprecision(2) << rec[index].fgrd;

        if (rec[index].fgrd >= 75 && rec[index].fgrd <= 100)
        {
            cout << setw(10) << "Passed\n\n";
        }

        else if (rec[index].fgrd < 75)
        {
            cout << setw(10) << "Failed\n\n";
        }
    }

    else
    {
        cout << "\t| |\t\t\t This record does not exist. Check your ID and try again.";
        goto check;
    }
}

else
{
    cout << "\t| |\t\t\t There is no current record to calculate a final grade.\n\n";
    return;
}
    }

而这是view_rec函数的代码:

    void view_rec(StudRec rec[], int studcount)
    {
if (studcount > 0)
{
    cout << "\n\n\t| |\t\t         ===================================================\n";
    cout << "\t| |\t\t\t\t         VIEW STUDENT RECORD \n";
    cout << "\t| |\t\t         ===================================================\n\n";
    int i=0;
    cout << "\n\t| |\t\t         " << left << setw(10) << "ID" << setw(30) << "NAME" << setw(7) << "SEX" << setw(10) << "QUIZ";
    cout << setw(14)<< "ASSIGNMENT" << setw(11) << "MIDTERM" << setw(14) << "FINAL EXAM" << setw(12) << "ACTIVITY";
    cout << setw(11)<< "PROJECT" << setw(9) << "TOTAL\n";
    cout << "\t| |\t\t         " << "----------------------------------------------------------------------------------------------------------------------------\n\n";

    while(i <= studcount)
    {
        if(rec[i].id != "")
        {

            cout << "\t| |\t\t         " << left << setw(10) << rec[i].id << setw(30) << rec[i].nm << setw(7) << rec[i].sex;
            cout << setw(10) << rec[i].quiz << setw(14) << rec[i].hw << setw(11) << rec[i].midT;
            cout << setw(14) << rec[i].fExam << setw(12) << rec[i].acts << setw(11) << rec[i].proj << setw(9) << rec[i].total;
            cout << endl << endl;
        }
        i+=1;
    }
}

else
{
    cout << "\t| |\t\t\t There is no current record to view.\n\n";
    return;
}

}

© www.soinside.com 2019 - 2024. All rights reserved.