reading a string

This is a discussion on reading a string within the C Programming forums, part of the General Programming Boards category; I have this program to store info for 3 libraries and then to search for a book by author for ...

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    12

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

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    >Any suggestions?

    FAQ > How do I... (Level 1) > Get a line of text from the user/keyboard (C)
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2003
    Posts
    12
    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.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>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.

    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.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    Maybe because you're not using gets()?

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 10:20 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. IRC, reading the stream
    By Iyouboushi in forum C# Programming
    Replies: 6
    Last Post: 08-03-2006, 06:34 PM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 04:58 PM
  5. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 06:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21