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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with gets()?

Status
Not open for further replies.

thoughts

IS-IT--Management
Jul 26, 2007
2
IN
Hi, may I know what is wrong with my codes?
SOmethhing went wrong when the first ISBN always cannot get through.DO i need to add an additional fflush()?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <float.h>

//initialisation
struct book_record
{
char ISBN[11];
char title[31];
char author[31];
char category[3];
char date[11];
float price;
};

struct BkArray
{
struct book_record BookArray[10];
};

//function prototypes
void add_records(BkArray *library);
void display_all_records(BkArray *library);
void display_information(char *ISBN, BkArray *library);
void display_books(BkArray *library);
void delete_records(char *title, BkArray *library);
void BarLine(int num);

BkArray library;
main()
{
// FILE *fp;
//variables
int choice;
char ISBN[11]; /*This ISBN refers to the input by the finder*/
char title[31];
char menu;

/* if((fp = fopen(&quot;test&quot;, &quot;r+t&quot;)) == NULL)
{
printf(&quot;\aError opening test\n&quot;);
exit(-1);
}
else
{
*/
BkArray library;
BkArray *lib;

for (int i = 0; i != 9; i++)
{
strcpy(library.BookArray.title, &quot;NULL&quot; );
strcpy(library.BookArray.author, &quot;NULL&quot; );
strcpy(library.BookArray.category, &quot;NULL&quot; );
strcpy(library.BookArray.date, &quot;NULL&quot; );
strcpy(library.BookArray.ISBN, &quot;NULL&quot; );
library.BookArray.price = 0.00;
}


do{

printf(&quot;\n&quot;);

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);

lib = &library;

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

printf(&quot;\nDo you want to go back to the menu? Press Y for yes.&quot;);
scanf(&quot;%c&quot;, &menu);

}while(menu == 'Y'||'y');
// }
// fclose(fp);

return 0;
}


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


//Add book records in Book Array
void add_records(BkArray* library)
{
//variables
int number;

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

// clscr();

for( int k = 0; k < number; k++)
{
printf(&quot;\n#%d Record:\n&quot;, k+1);
BarLine(30);
printf(&quot;Enter ISBN: &quot;);
gets( library->BookArray[k].ISBN);

printf(&quot;Enter the title of the book: &quot;);
gets( library->BookArray[k].title);
printf(&quot;Enter the author of the book: &quot;);
gets(library->BookArray[k].author);
printf(&quot;Enter the price of the book: &quot;);
scanf(&quot;%.2f&quot;, &library->BookArray[k].price);
printf(&quot;Enter the category of the book: &quot;);
gets( library->BookArray[k].category);
printf(&quot;Enter the published date in the format of dd/mm/yyyy: &quot;);
gets (library->BookArray[k].date);
}//for
}//add_records


//Display all book records
void display_all_records(BkArray *library)
{
for (int p = 0; p < sizeof(library); p++)
{
printf(&quot;\n#%d Record:\n&quot;, p+1);
BarLine(30);
printf(&quot;ISBN: %s.\n&quot;, library->BookArray[p].ISBN);
printf(&quot;Title: %s.\n&quot;, library->BookArray[p].title);
printf(&quot;Author: %s.\n&quot;, library->BookArray[p].author);
printf(&quot;Price: %.2f.\n&quot;, library->BookArray[p].price);
printf(&quot;Category: %s.\n&quot;, library->BookArray[p].category);
printf(&quot;Published Date: %s.\n&quot;, library->BookArray[p].date);
}//for

}//display_records


// Find and display book information given the IBSN
void display_information(char *ISBN, BkArray *library)
{
int cnt = 0;

for (int q = 0; q < sizeof(library); q++)
{
if (strcmp(library->BookArray[q].ISBN,ISBN))
{
printf(&quot;Book Found!\n&quot;);
printf(&quot;ISBN: %s.\n&quot;, ISBN);
printf(&quot;Title: %s.\n&quot;, library->BookArray[q].title);
printf(&quot;Author: %s.\n&quot;, library->BookArray[q].author);
printf(&quot;Price: %.2f.\n&quot;, library->BookArray[q].price);
printf(&quot;Category: %s.\n&quot;, library->BookArray[q].category);
printf(&quot;Published Date: %s.\n&quot;, library->BookArray[q].date);
cnt++;
}//if
}//for

if(cnt == 0)
printf(&quot;Book cannot be found!&quot;);
}//display_information


//find all books written by a particular author
void display_books(BkArray *library)
{
//variable
char author1[31];
int cnt = 0;

printf(&quot;\nEnter author: &quot;);
gets(author1);

for(int r = 0; r < sizeof(library); r++)
{
if(strcmp(library->BookArray[r].author, author1))
{
printf(&quot;The title of the book is %s.\n&quot;, library->BookArray[r].title);
cnt++;
break;
}
}//for

if(cnt != 1)
printf(&quot;No Book is found!&quot;);

}//display_book


//Delete book record given the 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++)
strcpy(library->BookArray[j].title, library->BookArray[j++].title);
}//if
}//for
}//delete_record
 
You have posted a lot of code. Please be more specific and try to boil the problem down to a reasonable size.

One thing you should note however is that gets() can overrun the provided buffer.

Also since you are mixing scanf() and gets() you may have stray carriage return or line feed characters floating around in the input buffer where you do not expect them. I usually avoid this by the following code.

fflush(stdin) ;
fgets( buff, N, stdin ) ;

Hope this helps.

Brudnakm
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top