Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 27th, 2005, 10:11 AM   #1
quantz
Newbie
 
Join Date: Feb 2005
Posts: 1
Rep Power: 0 quantz is on a distinguished road
string problem when passing in linked list

this is my program that have problem when displaying the current data in node, the integer and float data can be view prefectly but the problem at the string data where it not display the data that input but the output is something that like a line that not give any meaning. i hope somebody can help me to solve this problem.
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
#define size 15

struct book
{
char bookName[size],authorName[size];
int refNo;
float price;
struct book *next,*prev;
} *head=NULL,*newnode ,*Traverse;

insertData(char[15], char[15] , int, float);
deleteData(char[]);
showData();


main()
{
char book[size],author[size];
char i;
int ref;
float price;

FILE *fpt;
fpt=fopen("book.txt","w");


do{
system("cls");//clrscr()

printf("\t\t-----Welcome To Book Store System-----");

printf("\n\t\t [1] Insert Data");
printf("\n\t\t [2] Delete Data");
printf("\n\t\t [3] Display Information");
printf("\n\t\t [4] Exit System");

printf("\n\t\t Please Enter Your Choice: ");
scanf(" %c",&i);

switch(i)
{
case '1':
printf("Enter Book Name:");
scanf("\n%s",&book[size]);
printf("\nEnter Author Name:");
scanf ("\n%s",&author[size]);
printf("\nEnter Reference Number:");
scanf("\n %d",&ref);
printf("\nEnter Price: RM ");
scanf("\n%f",&price);

insertData(book, author, ref, price);

break;



case '2':
printf("Enter The Book Name You Want To Delete:");
scanf("\n%2s",&book);
deleteData(book);

break;

case '3':
showData();

break;


case '4':
printf("\nThanks You\n");
exit (0);

default:printf("\nInvalid Entry!Please Choose Between 1-4 Only");

getch();

}
}while (i!='4');

return 0;

}





//----------------------------------input data-----------------------------------------------
insertData(char book[size], char author[size], int ref, float price)
{
FILE *fpt;
//open file
fpt=fopen("book.txt","w");

if (head == NULL)
{
newnode = (struct book *)malloc (sizeof(struct book));
newnode -> bookName[size] = book[size];
newnode -> authorName[size] = author[size];
newnode -> refNo = ref;
newnode -> price = price;

newnode -> next = newnode -> prev = NULL;
head = newnode;

//write to file
fprintf(fpt,"\nBook name :%2s",newnode->bookName);
fprintf(fpt,"\nAuthor Name :%2s",newnode->authorName);
fprintf(fpt,"\nReference Number:%d",newnode->refNo);
fprintf(fpt,"\nPrice :RM%.2f",newnode->price);
}

else
{
newnode = (struct book *)malloc (sizeof(struct book));

newnode -> bookName[size] = book[size];
newnode -> authorName[size] = author[size];
newnode -> refNo = ref;
newnode -> price = price;

newnode -> next = newnode -> prev = NULL;

//write to file
fprintf(fpt,"\nBook name :%2s",newnode->bookName);
fprintf(fpt,"\nAuthor Name :%2s",newnode->authorName);
fprintf(fpt,"\nReference Number:%d",newnode->refNo);
fprintf(fpt,"\nPrice :RM%.2f",newnode->price);

Traverse = head;
while(Traverse -> next != NULL)

Traverse = Traverse -> next;

Traverse -> next = newnode;
newnode -> prev = Traverse;
}
fclose(fpt);

return 0;
}

//----------------------------------delete data-----------------------------------------------
deleteData(char book[])
{

Traverse = head;
if(Traverse ->bookName[size] == book[size])
{
head = head -> next;

free(Traverse);

printf("\nRecord Deleted for %2s",book);

}

else
{
while(Traverse -> bookName[size] !=book[size])
Traverse = Traverse -> next;

Traverse -> next -> prev = Traverse -> prev;
Traverse -> prev -> next = Traverse -> next;
free(Traverse);
printf("\nRecord Deleted for %2s",book);
}

getch();
return 0;
}

//----------------------------------display data-----------------------------------------------
showData()
{
Traverse = head;
if(Traverse != NULL){
while (Traverse !=NULL)
{

printf("Book Name :%s\nAuthor Name:%s\nReference Number:%d\nPrice:RM%.2f\n\n",Traverse->bookName,Traverse
->authorName,Traverse->refNo,Traverse->price);

Traverse = Traverse -> next;

}
}

else
{
printf("Sorry No Data To Show");
}

getch();
return 0;
}
quantz is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 5:57 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC