Thread: getting this example to compile

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    380

    getting this example to compile

    I'm using the Turbo C 2.01 compiler for this example that I copied from the book "Hands - on Turbo C" by Larry Joel Goldstein and Larry Gritz. It won't compile because of this line:

    if Entry.Name = SearchName then

    I tried fix the error myself, but I don't know how to convert the above line to C. It looks like qbasic to me.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <fcntl.h>
    #include <io.h>
    #include <sys\stat.h>
    
    typedef struct {
    	char Name[20];
    	char Number[12];
    } PhoneNum;
    
    int Directory;
    int accessmode;
    PhoneNum Entry;
    char SearchName[20];
    int Found;
    
    main()
    { /* PhoneSearch */
    	printf("Whose phone number to search for? ");
    	gets (SearchName);
    	accessmode = O_RDONLY | O_BINARY;
    	Directory = open("PHONE", accessmode);
    	Found = 0;
    	while(!Found && !eof(Directory))
    	             if (!strcmp(SearchName,Entry.Name))
    /* if Entry.Name = SearchName then */
    		   Found = 1;
    	             else
    		   read(Directory,&Entry,sizeof(Entry));
    	close(Directory);
    	if(Found)
    	   printf("Successful: %s", Entry.Number);
    	else
    	   printf("Unsucessful\n");
    } /* PhoneSearch */

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    That section that is commented out is just that. It's simply a comment. It is telling you what the above line is doing. See:

    if (!strcmp(SearchName,Entry.Name))
    /* if Entry.Name = SearchName then */


    The second line is explaining what the line right above it is doing.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    But the results aren't the same.

    The program is suppose to search through a file for a person's name and output their phone number.

    The file "PHONE" looks something like this:
    Dr. Fine 222-2222

    and user inputs a search for 'Dr. Fine' and if found the program should output 'success: 222-2222'

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you using low level file operators? fopen fread fclose should work fine. What you have to keep in mind here is that "strcmp" is case sensitive. Your case has to be an exact match. You could modify the code to see what the record holds by taking the 'else' statment and adding an output line to show you what the record holds.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    I copied the example from a book I'm trying to learn from.


    Maybe posting the exact code unmodified from the book will show you better what my problem is.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <fcntl.h>
    #include <io.h>
    #include <sys\stat.h>
    
    typedef struct {
    	char Name[20];
    	char Number[12];
    } PhoneNum;
    
    int Directory;
    int accessmode;
    PhoneNum Entry;
    char SearchName[20];
    int Found;
    
    main()
    { /* PhoneSearch */
    	printf("Whose phone number to search for? ");
    	gets (SearchName);
    	accessmode = O_RDONLY | O_BINARY;
    	Directory = open("PHONE", accessmode);
    	Found = 0;
    	while(!Found && !eof(Directory))
    	if Entry.Name = SearchName then
    		   Found = 1;
    	             else
    		   read(Directory,&Entry,sizeof(Entry));
    	close(Directory);
    	if(Found)
    	   printf("Successful: %s", Entry.Number);
    	else
    	   printf("Unsucessful\n");
    } /* PhoneSearch */

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    What book is this from? It seems like the author is an idiot to me

    >if Entry.Name = SearchName then
    Code:
    if ( strcmp ( Entry.Name, SearchName ) == 0 )
      Found = 1;
    else
      read ( Directory, &Entry, sizeof ( Entry ) );
    >gets (SearchName);
    Really bad, fgets is safer.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    The book is "Hands - on Turbo C" by Larry Joel Goldstein and Larry Gritz

  8. #8
    Registered User
    Join Date
    Aug 2001
    Posts
    380
    Ok. That solved that problem. Now I have another one.

    Let say "phone" contains:
    Dr. Fine 222-222
    Dr. Howard 333-333


    and Dr. Fine was the first entry and Dr. Howard is the last entry the program won't find the last entry.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C and C++ compile speed
    By swgh in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 01-02-2007, 02:37 PM
  2. Compile as you type
    By Rocketmagnet in forum A Brief History of Cprogramming.com
    Replies: 33
    Last Post: 12-07-2006, 01:36 PM
  3. How to compile mfc libs from platform sdk
    By tjcbs in forum Windows Programming
    Replies: 6
    Last Post: 11-19-2006, 08:20 AM
  4. Compile crashes certain windows
    By Loduwijk in forum C++ Programming
    Replies: 5
    Last Post: 03-26-2006, 09:05 PM
  5. How can I compile C or C++ with Visual Studio .NET?
    By Dakkon in forum C Programming
    Replies: 8
    Last Post: 02-11-2003, 02:58 PM