-
reading a string
I have this program to store info for 3 libraries and then to search for a book by author for the libraries.
My problem is with reading in the data for the libraries. It works fine for the code posted below. However, this only allows me to enter in titles of one word. I need to be able to read in titles with more than one word. If I try to switch to getchar() or gets() or something it reads a multi-word title for book 1 but then wont read anything for the title of the other books.
Any suggestions?
Code:
#include <stdio.h>
#include <string.h>
#define name_len 10
#define title_len 40
struct book {
int booknum;
char title[title_len+1];
char author[name_len+1];
int pagenum;
}book1, book2, book3, book4;
struct library {
struct book book[4];
} liba, libb, libc;
struct library getdata(struct library lib);
void search(struct library lib1, struct library lib2, struct library lib3);
main()
{
int i,j,q=0, sal1[6]={0}, sal2[5]={0};
struct book book[4];
struct library lib[3];
printf("Enter the details for Library A :\n\n");
lib[0] = getdata(lib[0]);
printf("Enter the details for LIbrary B :\n\n");
lib[1] = getdata(lib[1]);
printf("Enter the details for Library C :\n\n");
lib[2] = getdata(lib[2]);
search(lib[0], lib[1], lib[2]);
}
struct library getdata(struct library lib)
{ int i,j,k;
for(i=0; i<4; i++)
{
lib.book[i].booknum=(i+1);
printf("Book #%d - Title :\n", lib.book[i].booknum);
scanf("%s", &lib.book[i].title);
printf("Book #%d - Author :\n", lib.book[i].booknum);
scanf("%s", &lib.book[i].author);
printf("Book #%d - Number of Pages :\n", lib.book[i].booknum);
scanf("%d", &lib.book[i].pagenum);
}
return lib;
}
void search(struct library lib1, struct library lib2, struct library lib3)
{
int i, j=0,m=0;
char name[name_len+1];
printf("\nEnter name of Author to search for :\n");
scanf("%s", &name);
printf("\n\nLib_A\n\n");
printf("---------------------------------------------\n");
printf("# Title Author No. of Pages\n");
printf("---------------------------------------------\n");
{for(i=0, j=0; i<4; i++)
for(m=0; name[m]!='\0';m++)
{if(lib1.book[i].author[m] == name[m] && name[m+1]=='\0')
{printf("%d %-12s %-15s %d\n", lib1.book[i].booknum, lib1.book[i].title, lib1.book[i].author, lib1.book[i].pagenum);
j++;}}}
if(j==0)
printf("\nNo books written by %s available in Lib_A", name);
else if(j!=0)
printf("\n%d book(s) written by %s available in Lib_A", j, name);
printf("\n\nLib_B\n\n");
printf("---------------------------------------------\n");
printf("# Title Author No. of Pages\n");
printf("---------------------------------------------\n");
{for(i=0, j=0; i<4; i++)
for(m=0; name[m]!='\0';m++)
{ if(lib2.book[i].author[m] == name[m] && name[m+1]=='\0')
{printf("%d %-12s %-15s %d\n", lib2.book[i].booknum, lib2.book[i].title, lib2.book[i].author, lib2.book[i].pagenum);
j++;}}}
if(j==0)
printf("\nNo books written by %s available in Lib_B", name);
else if(j!=0)
printf("\n%d book(s) written by %s available in Lib_B", j, name);
printf("\n\nLib_C\n\n");
printf("---------------------------------------------\n");
printf("# Title Author No. of Pages\n");
printf("---------------------------------------------\n");
{for(i=0, j=0; i<4; i++)
for(m=0; name[m]!='\0';m++)
{ if(lib3.book[i].author[m] == name[m] && name[m+1]=='\0')
{printf("%d %-12s %-15s %d\n", lib3.book[i].booknum, lib3.book[i].title, lib3.book[i].author, lib3.book[i].pagenum);
j++;}}}
if(j==0)
printf("\nNo books written by %s available in Lib_C", name);
else if(j!=0)
printf("\n%d book(s) written by %s available in Lib_C", j, name);
}
-
-
I know how to read text. The problem is I don't know why when I use gets() to read in the string, it wont allow the user to enter any of the book titles except the first one.
-
>>this only allows me to enter in titles of one word.
>>I know how to read text.
Are you sure about that second statement? ;) You're asking about how to read complete strings, right?
>>when I use gets()
Stop right there! gets() is old hat, don't use it. :D
If you mix scanf()/gets()/fgets() you'll end up in trouble, especially if you're mixing strings and numbers (%s and %d in scanf).
The best solution would be to use fgets() for all input reading, the use a conversion function to convert the data to a number if required.
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
>>struct library getdata(struct library lib)
You should be passing struct pointers, not structs. Doing it your way is inefficient and will cause problems are the struct gets bigger.
>>scanf("%s", &lib.book[i].title);
Hopefully you'll move away from scanf(), but if you don't, this statement is incorrect. As title is an array name, you don't need to use the & to get its address.
-
Maybe because you're not using gets()?
gg