Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

who can correct error for me? 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
hi, there this program wrote with C programming, who can help me to debug it, thank n cheers




#include <stdio.h>
#include <string.h>
typedef enum
{
SF_Science_Fiction=0, HR_Horror=1, RM_Romance=2
} Category_Value;

typedef struct
{
char ISBN[10];
char Title[30];
char Author[30];
float Price;
Category_Value Category;
char PublishedDate[11];
} BkArray;


void add_records(BkArray []);
void display_all_records(BkArray []);
void display_information(char *IBSN, BkArray []);
void display_books(BkArray []);
void delete_records(char *title, BkArray library);
void BarLine(int num);

main()
{
//variables
int choice;
char ISBN[10];

BkArray library[10];

BarLine(30);
printf(&quot;\tMENU\n&quot;);
BarLine(30);
printf(&quot;1 - Add new records.\n&quot;);
printf(&quot;2 - Display all book records.\n&quot;);
printf(&quot;3 - Find and display book information given the ISBN.\n&quot;);
printf(&quot;4 - Find all books written by a particular author.\n&quot;);
printf(&quot;5 - Delete book record given the title.\n&quot;);
printf(&quot;\nEnter your choice: &quot;);
scanf(&quot;%d&quot;, &choice);

switch(choice)
{
case 1: add_records(library);
break;
case 2: display_all_records(library);
break;
case 3: printf(&quot;Enter ISBN: &quot;);
scanf(&quot;%s&quot;, ISBN);
break;
case 4: display_books(library);
break;
case 5: printf(&quot;Enter title: &quot;);
break;
default: printf(&quot;\nThere is no such choice.\n&quot;);
}

return 0;
}


void BarLine(int num)
{
for(int l = 0; l < num; l++)
printf(&quot;=&quot;);
printf(&quot;\n&quot;);
}



void add_records(BkArray lib[])
{

int number;

printf(&quot;\nEnter the number of records that you are going to enter: &quot;);
scanf(&quot;%d&quot;, &number);

for(int k = 0; k < number; k++)
{
printf(&quot;\n#%d Record:\n&quot;, k+1);
BarLine(30);
printf(&quot;Enter ISBN: &quot;);

gets( lib[k].ISBN);
printf(&quot;\nEnter the title of the book: &quot;);
gets( lib[k].Title);
printf(&quot;\nEnter the author of the book: &quot;);
gets(lib[k].Author);
printf(&quot;\nEnter the price of the book: &quot;);
scanf(&quot;%f&quot;, &lib[k].Price);
printf(&quot;\nEnter the category of the book\n&quot;);
scanf(&quot;%d&quot;, &lib[k].Category);
printf(&quot;\nEnter the published date in the format of dd/mm/yyyy: &quot;);
gets (lib[k].PublishedDate);
}
}



void display_all_records(BkArray library[])
{
for (int p = 0; p != 9; p++)
{
printf(&quot;\n#%d Record:\n&quot;, p+1);
BarLine(30);
printf(&quot;ISBN: %s\n&quot;, library[p].ISBN);
printf(&quot;Title: %s\n&quot;, library[p].Title);
printf(&quot;Author: %s\n&quot;, library[p].Author);
printf(&quot;Price: %f\n&quot;, library[p].Price);
printf(&quot;Category: %s\n&quot;, library[p].Category);
printf(&quot;Published Date: %s\n&quot;, library[p].PublishedDate);
}
return;
}



void display_information(char *ISBN, BkArray library[])
{
for (int q = 0; q != 9; q++)
{
if (strcmp(library[q].ISBN,ISBN))
{
printf(&quot;Book Found!&quot;);
printf(&quot;ISBN: %s\n&quot;, library[q].ISBN);
printf(&quot;Title: %s\n&quot;, library[q].Title);
printf(&quot;Author: %s\n&quot;, library[q].Author);
printf(&quot;Price: %s\n&quot;, library[q].Price);
printf(&quot;Category: %s\n&quot;, library[q].Category);
printf(&quot;Published Date: %s\n&quot;, library[q].PublishedDate);
return;
}
}
printf(&quot;Book cannot be found!&quot;);
}



void display_books(BkArray lib[])
{

char author1[31];

printf(&quot;Enter author: &quot;);
scanf(&quot;%s&quot;, &author1);

for(int r = 0; r != 9; r++)
{
if(lib[r].Author == author1)
printf(&quot;%s\n&quot;, lib[r].Title);
}

}



void delete_records(char *title, BkArray library)
{
for (int i = 0; i < sizeof(library.BookArray); i++)
{
if(title == library.BookArray.title)
{
for (int j = i; j < sizeof(library.BookArray); j++)
library.BookArray[j].title =
library.BookArray[j++].title;
}
}
}
 
dear flygod,

please point out your problem more exactly, i.e. what problem do you have with your code?

regards astrid
 
hi,astrid first of all thank for ur feedback to me.
in my program, when i compiled it, the error occur is on this part:
it always give me a error mess bookarray is not a member of 'BkArray' in function delete records(char *BkArray)

void delete_records(char *title, BkArray library)
{
for (int i = 0; i < sizeof(library.BookArray); i++)
{
if(title == library.BookArray.title)
{
for (int j = i; j < sizeof(library.BookArray); j++)
library.BookArray[j].title =
library.BookArray[j++].title;
}
}
}

 
Hi,

this version compiles at least, although I haven't performed any testing. BTW, you can't compare and copy arrays quite so simply.


#include <stdio.h>
#include <string.h>
typedef enum
{
SF_Science_Fiction=0, HR_Horror=1, RM_Romance=2
} Category_Value;

typedef struct
{
char ISBN[10];
char Title[30];
char Author[30];
float Price;
Category_Value Category;
char PublishedDate[11];
} BkArray;


void add_records(BkArray []);
void display_all_records(BkArray []);
void display_information(char *IBSN, BkArray []);
void display_books(BkArray []);
void delete_records(char *title, BkArray library);
void BarLine(int num);

main()
{
//variables
int choice;
char ISBN[10];

BkArray library[10];

BarLine(30);
printf(&quot;\tMENU\n&quot;);
BarLine(30);
printf(&quot;1 - Add new records.\n&quot;);
printf(&quot;2 - Display all book records.\n&quot;);
printf(&quot;3 - Find and display book information given the ISBN.\n&quot;);
printf(&quot;4 - Find all books written by a particular author.\n&quot;);
printf(&quot;5 - Delete book record given the title.\n&quot;);
printf(&quot;\nEnter your choice: &quot;);
scanf(&quot;%d&quot;, &choice);

switch(choice)
{
case 1: add_records(library);
break;
case 2: display_all_records(library);
break;
case 3: printf(&quot;Enter ISBN: &quot;);
scanf(&quot;%s&quot;, ISBN);
break;
case 4: display_books(library);
break;
case 5: printf(&quot;Enter title: &quot;);
break;
default: printf(&quot;\nThere is no such choice.\n&quot;);
}

return 0;
}


void BarLine(int num)
{
for(int l = 0; l < num; l++)
printf(&quot;=&quot;);
printf(&quot;\n&quot;);
}



void add_records(BkArray lib[])
{

int number;

printf(&quot;\nEnter the number of records that you are going to enter: &quot;);
scanf(&quot;%d&quot;, &number);

for(int k = 0; k < number; k++)
{
printf(&quot;\n#%d Record:\n&quot;, k+1);
BarLine(30);
printf(&quot;Enter ISBN: &quot;);

gets( lib[k].ISBN);
printf(&quot;\nEnter the title of the book: &quot;);
gets( lib[k].Title);
printf(&quot;\nEnter the author of the book: &quot;);
gets(lib[k].Author);
printf(&quot;\nEnter the price of the book: &quot;);
scanf(&quot;%f&quot;, &lib[k].Price);
printf(&quot;\nEnter the category of the book\n&quot;);
scanf(&quot;%d&quot;, &lib[k].Category);
printf(&quot;\nEnter the published date in the format of dd/mm/yyyy: &quot;);
gets (lib[k].PublishedDate);
}
}



void display_all_records(BkArray library[])
{
for (int p = 0; p != 9; p++)
{
printf(&quot;\n#%d Record:\n&quot;, p+1);
BarLine(30);
printf(&quot;ISBN: %s\n&quot;, library[p].ISBN);
printf(&quot;Title: %s\n&quot;, library[p].Title);
printf(&quot;Author: %s\n&quot;, library[p].Author);
printf(&quot;Price: %f\n&quot;, library[p].Price);
printf(&quot;Category: %s\n&quot;, library[p].Category);
printf(&quot;Published Date: %s\n&quot;, library[p].PublishedDate);
}
return;
}



void display_information(char *ISBN, BkArray library[])
{
for (int q = 0; q != 9; q++)
{
if (strcmp(library[q].ISBN,ISBN))
{
printf(&quot;Book Found!&quot;);
printf(&quot;ISBN: %s\n&quot;, library[q].ISBN);
printf(&quot;Title: %s\n&quot;, library[q].Title);
printf(&quot;Author: %s\n&quot;, library[q].Author);
printf(&quot;Price: %s\n&quot;, library[q].Price);
printf(&quot;Category: %s\n&quot;, library[q].Category);
printf(&quot;Published Date: %s\n&quot;, library[q].PublishedDate);
return;
}
}
printf(&quot;Book cannot be found!&quot;);
}



void display_books(BkArray lib[])
{

char author1[31];

printf(&quot;Enter author: &quot;);
scanf(&quot;%s&quot;, &author1);

for(int r = 0; r != 9; r++)
{
if(lib[r].Author == author1)
printf(&quot;%s\n&quot;, lib[r].Title);
}

}



void delete_records(char *title, BkArray library[])
{
for (int i = 0; i < 10; i++)
{
if(strcmp(title,library.Title)==0)
{
for (int j = i; j < 9; j++)
strcpy(library[j].Title, library[j++].Title);
}
}
}
:) Hope that this helped! ;-)
 
hi, daves thank for your help. after i compiled it, it run successfully. but i still have a few questions to need your advise. i can't understand what you mention that &quot;you can't compare and copy arrays quite so simply.&quot; mean what?? can u explain to me thank because i still very new in C languages.

second, when i compiles already, it will appear a main menu, after i enter the number records that i want to record, this program automatics jump to &quot;title of the book&quot;, actually it should go to enter &quot;ISBN&quot; first then &quot;title of the book&quot;.

third, i can't enter published date

that's all.thank for your help n nice day


 
dear chintseming,

as I see you got some help.

okay I think the problem that your programm jumps over the first thing (ISBN) to enter is that you use gets()
As: when you enter your choice. scanf reads the digit from the instream and lets the return in there.
now when gets() read the instream it finds the [return] in the instream and gets it. I don't now how exactly you can workaroud but try to do it by getting your input with scanf().

perhaps someone else can explain somewhat more clear?

there are some things I liked to mention.
1) You do not catch wrong input in your programm
What will happen if your user wants to input more than 11 records?
How does your user detemine what categories there are? What if he enters &quot;Science&quot;?
And users never behave as we want or expect them to !!!!

when deleting your records. you only compare the title of the book. Whithin my roundabout 6000 books there are some having the same title, by different authors. So your routine could kick out the wrong one.

regards astrid
 
The solution for the gets() problem with the remaining LF or CR in buffer can be solved by using scanf(&quot;%s&quot;,variable)
or by calling getchar() until it returns 0.

You could wirte a small function that empites the buffer,
containing somewhat like

while ( getchac() ) ;

perhaps you´ll have to use fgetchar, i don´t recall this so detailed.

 
thank all of you who's helped me...
between that, which of you expert in java languages?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top