为什么该程序在运行时给我“调试断言失败”错误消息

#include "stdafx.h"
#include "iostream"
#include "fstream"
#include "string"
using namespace std;

int conv_ch(char b)
{
    int f;
    f=b;
    b=b+0;
    switch(b)
    {
    case 48:
        f=0;
        break;
    case 49:
        f=1;
        break;
    case 50:
        f=2;
        break;
    case 51:
        f=3;
        break;
    case 52:
        f=4;
        break;
    case 53:
        f=5;
        break;
    case 54:
        f=6;
        break;
    case 55:
        f=7;
        break;
    case 56:
        f=8;
        break;
    case 57:
        f=9;
        break;
    default:
        f=0;
    }
    return f;
}

class Student
{
public:
    string id;
    size_t id_len;
    string first_name;
    size_t first_len;
    string last_name;
    size_t last_len;
    string phone;
    size_t phone_len;
    string grade;
    size_t grade_len;
    void print();
    void clean();
};
void Student::clean()
{
    id.erase (id.begin()+6, id.end());
    first_name.erase (first_name.begin()+15, first_name.end());
    last_name.erase (last_name.begin()+15, last_name.end());
    phone.erase (phone.begin()+10, phone.end());
    grade.erase (grade.begin()+2, grade.end());
}
void Student::print()
{
    int i;
    for(i=0;i<6;i++)
    {
        cout<<id[i];
    }
    cout<<endl;
    for(i=0;i<15;i++)
    {
        cout<<first_name[i];
    }
    cout<<endl;
    for(i=0;i<15;i++)
    {
        cout<<last_name[i];
    }
    cout<<endl;
    for(i=0;i<10;i++)
    {
        cout<<phone[i];
    }
    cout<<endl;
    for(i=0;i<2;i++)
    {
        cout<<grade[i];
    }
    cout<<endl;

}


int main()
{
    Student k[80];
    char data[1200];
    int length,i,recn=0;
    int rec_length;
    int counter = 0;
    fstream myfile;
    char x1,x2;
    char y1,y2;
    char zz;
    int ad=0;
    int ser,j;
    myfile.open ("example.txt",ios::in);
    int right;
    int left;
    int middle;
    string key;
    while(!myfile.eof()){
            myfile.get(data,1200);
            char * pch;
            pch = strtok (data, "#");
            printf ("%s\n", pch);
            j=0;
            for(i=0;i<6;i++)
            {
                k[recn].id[i]=data[j];
                j++;
            }
            for(i=0;i<15;i++)
            {
                k[recn].first_name[i]=data[j];
                j++;
            }
            for(i=0;i<15;i++)
            {
                k[recn].last_name[i]=data[j];
                j++;
            }
            for(i=0;i<10;i++)
            {
                k[recn].phone[i]=data[j];
                j++;
            }
            for(i=0;i<2;i++)
            {
                k[recn].grade[i]=data[j];
                j++;
            }
            recn++;
            j=0;
    }


    //cout<<recn;
    string temp1;
    size_t temp2;
    int temp3;
    for(i=0;i<recn-1;i++)
    {
        for(j=0;j<recn-1;j++)
        {
            if(k[i].id.compare(k[j].id)<0)
            {
                temp1 = k[i].first_name;
                k[i].first_name = k[j].first_name;
                k[j].first_name = temp1;

                temp2 = k[i].first_len;
                k[i].first_len = k[j].first_len;
                k[j].first_len = temp2;

                temp1 = k[i].last_name;
                k[i].last_name = k[j].last_name;
                k[j].last_name = temp1;

                temp2 = k[i].last_len;
                k[i].last_len = k[j].last_len;
                k[j].last_len = temp2;

                temp1 = k[i].grade;
                k[i].grade = k[j].grade;
                k[j].grade = temp1;

                temp2 = k[i].grade_len;
                k[i].grade_len = k[j].grade_len;
                k[j].grade_len = temp2;

                temp1 = k[i].id;
                k[i].id = k[j].id;
                k[j].id = temp1;

                temp2 = k[i].id_len;
                k[i].id_len = k[j].id_len;
                k[j].id_len = temp2;

                temp1 = k[i].phone;
                k[i].phone = k[j].phone;
                k[j].phone = temp1;

                temp2 = k[i].phone_len;
                k[i].phone_len = k[j].phone_len;
                k[j].phone_len = temp2;

            }
        }
    }
    for(i=0;i<recn-1;i++)
    {
    k[i].clean();
    }
    char z;
    string id_sear;
    cout<<"Enter 1 to display , 2 to search , 3 to exit:";
    cin>>z;
    while(1){
        switch(z)
        {
        case '1':
            for(i=0;i<recn-1;i++)
            {
                k[i].print();
            }
            break;
        case '2':
            cin>>key;
            right=0;
            left=recn-2;
            while(right<=left)
            {
                middle=((right+left)/2);
                if(key.compare(k[middle].id)==0){
                    cout<<"Founded"<<endl;
                    k[middle].print();
                    break;
                }
                else if(key.compare(k[middle].id)<0)
                {
                    left=middle-1;
                }
                else
                {
                    right=middle+1;
                }
            }
            break;
        case '3':
            exit(0);
            break;

        }
        cout<<"Enter 1 to display , 2 to search , 3 to exit:";
        cin>>z;

    }

    return 0;
}


程序从文件example.txt中读取

313121crewwe         matt           0114323111A #
433444cristinaee     john        0113344325A+#
324311matte          richee          3040554032B #


这个想法是使用文本分隔符记录strucutre读取固定大小的字段结构

最佳答案

断言失败由您使用无效参数调用的标准C ++库函数之一引发。在调试器中运行它,您应该确切看到什么调用。

看起来很可疑的一件事:在Student::clean中,您像这样拨打电话:

first_name.erase (first_name.begin()+15, first_name.end());


如果first_name的长度小于15,则可能导致断言失败或其他错误。

09-16 01:15