•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 402,430 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,959 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser: Programming Forums
Views: 1168 | Replies: 2
![]() |
•
•
Join Date: Mar 2006
Posts: 1
Reputation:
Rep Power: 0
Solved Threads: 0
um... i'm an IT student. i'm still a freshmen though... i'm kinda confused with c programming. i have to make this program using stack. i can add, delete, and view all records but i can't seem to make my search function work. here's my codes, hopw anyone can help. thanks! 
#include <stdio.h>
struct bookstore{
char title[15];
char author[15];
int booknum;
char publisher[15];
int quantity;
struct bookstore *end;
};
typedef struct bookstore *book;
book add, first, del,temp;
void insert(void);
void delete(void);
void viewall(void);
book search(void);
main()
{
int choice=0;
do
{
clrscr();
printf("Bookstore Data Entry System\n");
printf("\n[1] Add a Book");
printf("\n[2] Delete a Book");
printf("\n[3] Display Books from Stack");
printf("\n[4] Search");
printf("\n[5] Exit");
printf("\nChoice: ");
scanf("%d",&choice);
switch(choice){
case 1: insert(); break;
case 2: printf("\n\n\n\n");
printf("Latest Entry Deleted.");
delete();
break;
case 3: viewall(); break;
case 4: search(); break;
}
}while(choice != 5);
getch();
}
void insert(void)
{
add = (book)malloc(sizeof(struct bookstore)); fflush(stdin);
printf("\nBook Number: "); scanf("%d",&add->booknum); fflush(stdin);
printf("\nBook Title: "); gets(add->title); fflush(stdin);
printf("\nAuthor: "); gets(add->author); fflush(stdin);
printf("\nQuantity: "); scanf("%d",&add->quantity); fflush(stdin);
printf("\nPublisher: "); gets(add->publisher); fflush(stdin);
add->end = first;
first = add;
}
void delete(void)
{
del = first;
first = first->end;
free(del);
getch();
}
void viewall(void)
{
del = first;
printf("\nBookstore Data Entry System\n\n");
while(del)
{
printf("\nBook Number: %s",del->booknum);
printf("\nBook Title: %s",del->title);
printf("\nAuthor: %s",del->author);
printf("\nQuantity: %d",del->quantity);
printf("\nPublisher: %s",del->publisher);
printf("\n\n");
del = del->end;
}
printf("\n\n");
system("PAUSE");
}
book search(void)
{
int i;
printf("\n\nEnter book number to be SEARCHED: ");
scanf("%d",&i);
booknum = &i;
temp = first;
while(temp != NULL)
{
if(temp->booknum == i)
printf("\nNo record");
else
temp = temp->bookstore;
++i;
}
printf("\nBook Number: %s",del->booknum);
printf("\nBook Title: %s",del->title);
printf("\nAuthor: %s",del->author);
printf("\nQuantity: %d",del->quantity);
printf("\nPublisher: %s",del->publisher);
printf("\n\n");
}

#include <stdio.h>
struct bookstore{
char title[15];
char author[15];
int booknum;
char publisher[15];
int quantity;
struct bookstore *end;
};
typedef struct bookstore *book;
book add, first, del,temp;
void insert(void);
void delete(void);
void viewall(void);
book search(void);
main()
{
int choice=0;
do
{
clrscr();
printf("Bookstore Data Entry System\n");
printf("\n[1] Add a Book");
printf("\n[2] Delete a Book");
printf("\n[3] Display Books from Stack");
printf("\n[4] Search");
printf("\n[5] Exit");
printf("\nChoice: ");
scanf("%d",&choice);
switch(choice){
case 1: insert(); break;
case 2: printf("\n\n\n\n");
printf("Latest Entry Deleted.");
delete();
break;
case 3: viewall(); break;
case 4: search(); break;
}
}while(choice != 5);
getch();
}
void insert(void)
{
add = (book)malloc(sizeof(struct bookstore)); fflush(stdin);
printf("\nBook Number: "); scanf("%d",&add->booknum); fflush(stdin);
printf("\nBook Title: "); gets(add->title); fflush(stdin);
printf("\nAuthor: "); gets(add->author); fflush(stdin);
printf("\nQuantity: "); scanf("%d",&add->quantity); fflush(stdin);
printf("\nPublisher: "); gets(add->publisher); fflush(stdin);
add->end = first;
first = add;
}
void delete(void)
{
del = first;
first = first->end;
free(del);
getch();
}
void viewall(void)
{
del = first;
printf("\nBookstore Data Entry System\n\n");
while(del)
{
printf("\nBook Number: %s",del->booknum);
printf("\nBook Title: %s",del->title);
printf("\nAuthor: %s",del->author);
printf("\nQuantity: %d",del->quantity);
printf("\nPublisher: %s",del->publisher);
printf("\n\n");
del = del->end;
}
printf("\n\n");
system("PAUSE");
}
book search(void)
{
int i;
printf("\n\nEnter book number to be SEARCHED: ");
scanf("%d",&i);
booknum = &i;
temp = first;
while(temp != NULL)
{
if(temp->booknum == i)
printf("\nNo record");
else
temp = temp->bookstore;
++i;
}
printf("\nBook Number: %s",del->booknum);
printf("\nBook Title: %s",del->title);
printf("\nAuthor: %s",del->author);
printf("\nQuantity: %d",del->quantity);
printf("\nPublisher: %s",del->publisher);
printf("\n\n");
}
•
•
Join Date: Jun 2004
Posts: 125
Reputation:
Rep Power: 5
Solved Threads: 1
i think so the program is perfect check if there is an linking problem from your side
>i think so the program is perfect
Then you're not qualified to help. The code is *horrible* in so many ways. However, because I'm pressed for time, I'll refrain from describing all of the problems in detail and focus only on the search function.
>booknum = &i;
What's this?
>if(temp->booknum == i)
>printf("\nNo record");
Wait, you're searching for a book number equal to i, so why do you say there are no records when you find it?
>printf("\nBook Number: %s",del->booknum);
booknum is an integer. Printing it as a string is bad news. Also, at this point del will be a null pointer, which is even more bad news.
>++i;
Why on earth are you modifying your search key?
Try this instead:
Of course, it's much better to avoid doing I/O in a utility function. You would be better off passing the search key as an argument and returning the found item, or NULL. Then the calling function can use that information as it pleases rather than having the search function force potentially undesired behavior:
Then you're not qualified to help. The code is *horrible* in so many ways. However, because I'm pressed for time, I'll refrain from describing all of the problems in detail and focus only on the search function.
>booknum = &i;
What's this?
>if(temp->booknum == i)
>printf("\nNo record");
Wait, you're searching for a book number equal to i, so why do you say there are no records when you find it?
>printf("\nBook Number: %s",del->booknum);
booknum is an integer. Printing it as a string is bad news. Also, at this point del will be a null pointer, which is even more bad news.
>++i;
Why on earth are you modifying your search key?
Try this instead:
book search(void)
{
int i;
printf("\n\nEnter book number to be SEARCHED: ");
scanf("%d",&i);
temp = first;
while(temp != NULL) {
if(temp->booknum == i) {
printf("\nBook Number: %d",temp->booknum);
printf("\nBook Title: %s",temp->title);
printf("\nAuthor: %s",temp->author);
printf("\nQuantity: %d",temp->quantity);
printf("\nPublisher: %s",temp->publisher);
printf("\n\n");
break;
}
temp = temp->end;
}
if ( temp == NULL )
printf ( "No record\n" );
return temp;
}book search(int key)
{
temp = first;
while ( temp != NULL ) {
if ( temp->booknum == key )
break;
temp = temp->end;
}
return temp;
} I'm a programmer. My attitude starts with arrogance, holds steady at condescension, and ends with hostility. Get used to it.
![]() |
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
adsense adult advertising blackberry blogging bomb book business coding copyright development earth engine environment failure forum google internet legal malware marketing mcafee microsoft monetization msn news operating pagerank phishing privacy publishing revenue search security seo sex silverlight software space spyware support survey system technical video web webmaster wiki work yahoo
- using Graphic.h (C++)
- Java 5 with Mac OS X (Java)
- Sorting a 2d array of strings in C++?? (C++)
- homepage hijack please help! (Viruses, Spyware and other Nasties)
- Having trouble trying to use a global structure (C)
- Oh Noooooooo!! (Viruses, Spyware and other Nasties)
- can anyone assiat in getting my function to work? (C)
Other Threads in the C Forum
- Previous Thread: printing double array formatting trouble
- Next Thread: Small problem with command line



Linear Mode