String Tokenization!!!!!! URGENT!!!

Hii this is my String tokenization code......when i debug..it shows badptr ...PLZZ i need help as this is DUE tomorrow!!!!

Book *Getinfo(char *b)//Tokenization function.
{
int ypub;
Book *bp= new Book;
int n = 300;
/*char *ptr = new char[n];*/
char *ptr = strtok(b,"/");
//----------title--------------
if(ptr== NULL)
return NULL;
else{
bp->title = new char [20];
strcpy(bp->title,ptr);
}
ptr = strtok(NULL,"/");
//-----------isdn----------------
if(ptr==NULL)
return NULL;
else
strcpy(bp->ISDN,ptr);
ptr = strtok(NULL,"/");
int i = atoi(ptr);
//--------------btheme-------------
if(ptr==NULL)
return NULL;
else
if (i = 0)
bp->btheme==ACADEMIC;
if(i =1)
bp->btheme==FICTION;
if(i =2)
bp->btheme==COMEDY;
else
bp->btheme==TRAGEDY;
ptr = strtok(NULL,"/");
int j = atoi(ptr);
//----------status-----------------
if(ptr==NULL)
return NULL;
else
if (j = 0)
bp->status==ON_SHELF;
if(j = 1)
bp->status==RESERVED;
if(j = 2)
bp->status==CHECKED_OUT;
else
bp->status==DELETED;
ptr = strtok(NULL,"/");
//------------author--------------
if(ptr==NULL)
return NULL;
else
{
bp->author= new char [50];
strcpy(bp->author,ptr);
}
ptr = strtok(NULL,"/");
//-------------------publisher----------------
if(ptr==NULL)
return NULL;
else
{
bp->publisher=new char[50];
strcpy(bp->publisher,ptr);
}
ptr = strtok(NULL,"/");
bp->ypublication=atoi(ptr);
//--------------------ypublication---------------------
if(ptr==NULL)
return NULL;
else
/* strcpy(bp->ypublication,ptr);*/
ptr = strtok(NULL,"/");
bp->edition=atoi(ptr);
//---------------------edition-------------------
if(ptr==NULL)
return NULL;
else
/*strcpy(bp->edition,ptr);*/
ptr = strtok(NULL,"/");
return bp;
}
Thnkss...but this uses vectors and algorithmss...we are suppose to do it without tht
You could put your code inside code tags [code][/code] so that code formatting is kept intact because it's very hard to read a lot of if statements without indentations.

How is the string pointed to by b allocated?
void main()
{
int n; // no of books in file
char buf[300];
ifstream fin("book.txt");
if (fin.fail())
{
cout<<"Error";
exit(1);
}
fin>>n;
Book** tpbooks = new Book* [2*n];
for(int i=0;i<n;i++)
{
fin.getline(buf,sizeof(buf),'\n');//reading in the whole line from the file.
tpbooks[i] = Getinfo(buf);

}

Borrow_Book(tpbooks,n);
/* Return_Book(tpbooks,n);
Add_Book(tpbooks,n);*/
fin.close();

}
if you call it like so: Book *b = Getinfo("Test/string");

It will crash (here: char *ptr = strtok(b,"/");) since "Test string" is constant and connot be modified

Unfortunately compilers allow passing const strings as non const strings.

You can avoid that crash like so:
1
2
3
4
5
6
7
int main()
{
...
  char text[] = "Test/string";
  Book *b = Getinfo(text);
...
}
Last edited on
but how will it wrk for the loop?
This is certainly wrong:
1
2
if (i = 0)
bp->btheme==ACADEMIC;

You want this:
1
2
if (i == 0)
bp->btheme=ACADEMIC;


The same applies for the rest

Here strcpy(bp->ISDN,ptr); you copy a string to 'ISDN'. Is it possible that 'ISDN' requires a new? Especially because you did this for 'title' above.
Last edited on
i'' fix it and seee =)
title requires a new because its a pointer.....im nt sure abt isdn ill chk it anyways =)
Another problem may be this:
1
2
ptr = strtok(NULL,"/");
int i = atoi(ptr);

You don't check if ptr is NULLwhen it comes to atoi()

Checking your code would really be easier if you use code tags: [code]Place your code here![/code]
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
 Book *Getinfo(char *b)//Tokenization function.
{
        int ypub;
        Book *bp= new Book;
                int n = 300;
               /* char *ptr = new char[n];*/
        char *ptr = strtok(b,"/");
//----------title--------------
        if(ptr== NULL)
                return NULL;
                else{
                        bp->title = new char [20];
                strcpy(bp->title,ptr);
                }
        ptr = strtok(NULL,"/");
//-----------isdn----------------
        if(ptr==NULL)
                return NULL;
        else
                strcpy(bp->ISDN,ptr);
        ptr = strtok(NULL,"/");
                int i = atoi(ptr);
//--------------btheme-------------
        if(ptr==NULL)
                return NULL;
        else
        if (i == 0)
                bp->btheme=ACADEMIC;
        if(i ==1)
                bp->btheme=FICTION;
        if(i ==2)
                bp->btheme=COMEDY;
        else
                bp->btheme=TRAGEDY;
        ptr = strtok(NULL,"/");   
                int j = atoi(ptr);
//----------status-----------------
        if(ptr==NULL)
                return NULL;
        else
        if (j == 0)
                bp->status=ON_SHELF;
        if(j == 1)
                bp->status=RESERVED;
        if(j == 2)
                bp->status=CHECKED_OUT;
        else
                bp->status=DELETED;
        ptr = strtok(NULL,"/");        
//------------author--------------
        if(ptr==NULL)
                return NULL;
        else
                {
                        bp->author= new char [50];
                strcpy(bp->author,ptr);
                }
        ptr = strtok(NULL,"/");
//-------------------publisher----------------
        if(ptr==NULL)
                return NULL;
        else
                {
                        bp->publisher=new char[50];
                strcpy(bp->publisher,ptr);
                }
        ptr = strtok(NULL,"/");
                bp->ypublication=atoi(ptr);
//--------------------ypublication---------------------
        if(ptr==NULL)
                return NULL;
        else
               /* strcpy(bp->ypublication,ptr);*/
        ptr = strtok(NULL,"/");
                bp->edition=atoi(ptr);
//---------------------edition-------------------
        if(ptr==NULL)
                return NULL;
        else
                /*strcpy(bp->edition,ptr);*/
        ptr = strtok(NULL,"/");
                return bp;
}
You didn't do anything on that atoi() problem. Or is your problem solved?
Topic archived. No new replies allowed.