string subcript out of range

Can someone check this code for me?
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
#include <iostream>
#include <conio.h>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
struct SINHVIEN{
       string name;
       string studentCard;
       string birthDay;
       string classCard;
       SINHVIEN *next;
};
SINHVIEN *pdau=NULL;
fstream myFile;
void Swap(SINHVIEN *p, SINHVIEN *q)
{
       SINHVIEN *tg;
       tg->name=p->name;
       tg->studentCard=p->studentCard;
       tg->birthDay=p->birthDay;
       tg->classCard=p->classCard;
       p->name=q->name;
       p->studentCard=q->studentCard;
       p->birthDay=q->birthDay;
       p->classCard=q->classCard;
       q->name=tg->name;
       q->studentCard=tg->studentCard;
       q->birthDay=tg->birthDay;
       q->classCard=tg->classCard;
}
void Input()
{
     string tam;
     SINHVIEN *p;
     myFile.open("student.txt",ios::in |ios::out | ios::app);
	 /*/
     if(myFile!=NULL)
     {            
         while(!myFile.eof())
         {
              getline(myFile,tam);               
              int vt=tam.find(',',0);
              if(pdau==NULL)
              {
                  pdau=new SINHVIEN;
                  p=pdau;         
                  }
              else {
                   p->next=new SINHVIEN;
                   p=p->next;
                   }
              p->name=tam.substr(0,vt);
              int i=vt;
              while(tam[++vt]!=',');
              p->studentCard=tam.substr(i+1,vt-i-1);
              p->birthDay=tam.substr(vt+1,tam.length()-1);
              p->next=NULL;                
         }          
     }
	 */
     while(1)
     {
             fflush(stdin);
             cout<<"\nEnter name(press Enter to exit)): ";
             getline(cin,tam);
             if(tam[0]=='\0')
                  break;
             if(pdau==NULL)
             {
                  pdau=new SINHVIEN;
                  p=pdau;         
             }
             else {
                  p->next=new SINHVIEN;
                  p=p->next;
                  }
             p->name=tam;
             cout<<"\nEnter studentCard: ";
             getline(cin,p->studentCard);
             cout<<"\nEnter day of birth: ";
             getline(cin,p->birthDay);
             myFile<<p->name<<","<<p->studentCard<<","<<p->birthDay;
             p->next=NULL;          
     }
     myFile.close();
}
void Output()
{
     SINHVIEN *p;
     p=pdau;
     system("cls");
     while(p!=NULL)
     {
         cout<<p->name<<" ";
         cout<<p->studentCard<<" ";
         cout<<p->birthDay<<endl;
         p=p->next;               
     }
     _getch();
}
int main()
{
    Input();
    Output();
}
1
2
3
string tam;
getline(cin,tam);
if(tam[0]=='\0') // kaboom 

std::string has member function empty()
this bug is located in this code. But I don't find out it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if(myFile!=NULL)
     {            
         while(!myFile.eof())
         {
              getline(myFile,tam);               
              int vt=tam.find(',',0);
              if(pdau==NULL)
              {
                  pdau=new SINHVIEN;
                  p=pdau;         
                  }
              else {
                   p->next=new SINHVIEN;
                   p=p->next;
                   }
              p->name=tam.substr(0,vt);
              int i=vt;
              while(tam[++vt]!=',');
              p->studentCard=tam.substr(i+1,vt-i-1);
              p->birthDay=tam.substr(vt+1,tam.length()-1);
              p->next=NULL;                
         }          
     }
1
2
3
4
int vt=tam.find( ',', 0 );
// at this point vt could be out of range, if there is no ','
while ( tam[++vt] != ',' ); // Why do you advance with while and not with find?
// again, if the first comma was the last character, or there is no second comma, then you fail 
Topic archived. No new replies allowed.