Abort Error

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* Program to create student record using primary key index usn */
#include<iostream>
#include<fstream>
#include<unistd.h>
#include<sstream>
#include<string>
#include<stdlib.h>

using namespace std;


class pi_index
{
   public: 
            string usn_list[100];
            int add_list[100];
            int count;
           string buffer;
           void insert();
            void sort_index();
           void search(string);
           int search_index(string);
           void remove(string);
             void create_index();
            string extract_usn(string);
};

void pi_index::insert()
{
  string buffer;
    int pos=0;
   fstream file;
   string usn,name,branch,sem;
   cout<<"USN :\n";
    cin>>usn;
   cout<<"Name :\n";
    cin>>name;
   cout<<"Branch :\n";
    cin>>branch;
  cout<<"Sem :\n";
    cin>>sem;

 file.open("5.txt",ios::out|ios::app);
  buffer+=usn+'|'+name+'|'+branch+'|'+sem+'$'+'\n';
  file<<buffer;
  file.close();
}

void pi_index::sort_index()
{
    int temp_address,i,j;
     string temp_usn;
     for(i=0;i<count-2;i++)
{
      for(j=0;j<count-i-2;j++)
 {
       if(usn_list[j]>usn_list[j+1])
{
        temp_usn=usn_list[j];
        usn_list[j]=usn_list[j+1];
          usn_list[j+1]=temp_usn;
           temp_address=add_list[j];
           add_list[j]=add_list[j+1];
       add_list[j+1]=temp_address;
 }
}
}
}

void pi_index::search(string key)
{
    fstream file;
     int pos,address;
      pos=search_index(key);
       if(pos!=999)
{
     buffer.erase();
     file.open("5.txt");
     address=add_list[pos];
      file.seekp(address,ios::beg);
      getline(file,buffer);
      cout<<"Record Found\n";
      cout<<buffer;
      file.close();
}
else
cout<<"File not found\n";
}

int pi_index::search_index(string key)
{
     int low=0,high=count,mid,pos,flag=0;
     while(low<=high)
{
     mid=(low+high)/2;
   if(key==usn_list[mid])
{
    flag=1;
     break;
 }
if(key<usn_list[mid])
  high=mid-1;
if(key>usn_list[mid])
  low=mid+1;
}
if(flag)
return mid;
else
return 999;
}

void pi_index::remove(string key)
{
    fstream file;
     int pos,address;
   pos=search_index(key);
     if(pos!=999)
{
   file.open("5.txt");
   address=add_list[pos];
   file.seekg(address,ios::beg);
   file.put('*');
   cout<<"Record Deleted\n";
   file.close();
}
cout<<"Record not found\n";
}

void pi_index::create_index()
{
   fstream file;
    int pos,i=0;
     string usn;
    
    int count=-1;
    file.open("5.txt",ios::in);
    while(!file.eof())
   {
      buffer.erase();
      pos=file.tellg();
     getline(file,buffer);
    if(buffer[i]!='*')
{
     if(buffer.empty()) break;
        usn=extract_usn(buffer);
        usn_list[++count]=usn;
        add_list[count]=pos;
}
}
file.close();
sort_index();
}

string pi_index::extract_usn(string buffer)
{
   string usn;
     int i=0;
     while(buffer[i]!='|')
       usn+=buffer[i++];
      return usn;
}
int main()
{
   
    int ch,no;
    string key;
     pi_index pi;

     while(1)

{
    pi.create_index();
    cout<<"Main Menu\n";
     cout<<"1.ADD 2.Search 3.Delete 4.Exit\n";
     cin>>ch;
    switch(ch)
{
    case 1:cout<<"Enter the no of records to insert\n";
           cin>>no;
        for(int i=0;i<no;i++)
       pi.insert();
         break;
  
     case 2:cout<<"Enter the key to be searched\n";
            cin>>key;
            pi.search(key);
             break;
      case 3:cout<<"Enter the key of the record to be deleted\n";
             cin>>key;
             pi.remove(key);
              break;
       default: exit(0);
  }
}
return 0;             
}


I'm getting the following error
1
2
3
terminate called after throwing an instance of 'std::length_error'
  what():  basic_string::_S_create
Aborted

when i debug,it says it points out to extract_usn function.but i'm not able to figure out the problem.Plz help.













That function attempts to extract non-existent characters if the passed string doesn't contain the character '|'. You must also stop looping if you reach the end of the string without finding the said character.
Topic archived. No new replies allowed.