Mar 28, 2012 at 1:56pm UTC
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;
}
Mar 28, 2012 at 2:22pm UTC
Thnkss...but this uses vectors and algorithmss...we are suppose to do it without tht
Mar 28, 2012 at 2:29pm UTC
You could put your code inside code tags [co de][/co de] 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?
Mar 28, 2012 at 2:35pm UTC
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();
}
Mar 28, 2012 at 2:37pm UTC
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 Mar 28, 2012 at 2:38pm UTC
Mar 28, 2012 at 2:43pm UTC
but how will it wrk for the loop?
Mar 28, 2012 at 2:59pm UTC
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 Mar 28, 2012 at 3:00pm UTC
Mar 28, 2012 at 4:26pm UTC
i'' fix it and seee =)
title requires a new because its a pointer.....im nt sure abt isdn ill chk it anyways =)
Mar 30, 2012 at 6:58am UTC
You didn't do anything on that atoi() problem. Or is your problem solved?