Thread: Linear search function accessing string in a struct

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    11

    Linear search function accessing string in a struct

    I currently have a file which allows inputs to record different transistor types. I then have the task of accessing this structure, find a certain manufacturer ID, and print the information about this particular transistor.

    My problem is accessing the array to search through.
    Here is my code:
    Code:
    #include "stdio.h"
    const int IDLEN=30; //All constant values defined
    const int POLARITYLEN=3;
    const int MAXSTOCKITEMS=10;
        
    //First structure defined
    struct TransistorRec {
    	char manufacturersID[IDLEN];
    	char polarity[POLARITYLEN];
    	float power;
    	float gain;
    	int stock;
    };
    
    
    //Function prototype
    int linearSearch ( struct TransistorRec ID[], const char *searchKey, int arraySize);
    
    
    int main()
    {
    	int total,i;
    	char ManIDS;
    	char manarray[IDLEN];
    	TransistorRec a[10];
    	//Ask user how many transistors are in stock
    	printf("How many different transistors are in stock?  ");
    	scanf("%i",&total);
    	
    	//If statment to make sure that not too many items are in stock
    	if(total>MAXSTOCKITEMS)
    	{
         printf("To many items in stock, choose a smaller value: ");
         scanf("%i",&total);
         }
         
    	//Ask user to enter in all neccesary data
    	for ( i = 0; i <total; i++){
    		printf("Enter manufacturer's ID, polarity, power, gain, stock :\n");
    	scanf("%s %s %f %f %i",a[i].manufacturersID,a[i].polarity,&a[i].power,&a[i].gain,&a[i].stock);
    	}
    	
    	//Now to print this information in a tabular format
    	//Print all required headers
    	printf("\n\nManID\tPolarity\tMax Power\tCurrent Gain\tNo. in stock");
    	
    	//Now to print out all data using a loop
    	for ( i = 0; i <total; i++)
    	{
            printf("\n%s \t%s \t\t%4.1f \t\t%4.1f \t\t%i",a[i].manufacturersID,a[i].polarity,a[i].power,a[i].gain,a[i].stock);
            }
        //Search for manufacxturer ID
    
    
         printf("Enter manufacturer ID to search for: ");
         scanf("%s",&ManIDS);
         int index = linearSearch ( struct TransistorRec ID[], ManIDS, total);
    
    
        if (index < 0) { // search string was not found
            printf("Search string not found!\n");
        }
        else {
            printf("String %s found at index %d\n", manufacturersID[index], index);
        }
    
    
     
    
    
    	return 0;
    	
    }
    My search function is as follows:
    Code:
    #include "stdio.h"
    
    
    int linearSearch (struct TransistorRec ID[], const char *searchKey, int arraySize) {
        for (int i = 0; i < arraySize; ++i) {
            if (searchKey == ID[i].manufacturersID)
                return i;
        }
    
    
        // We didn't find the searchKey in the Array
        return -1;
    }
    The errors I am currently getting are on line 54 'expected primary-expression before "struct"' and on line 60 ' 'maunfacturersID' undeclared'

    I am very new to the c language so sorry if my code i sloopy and/or confusing.

    Any help on how I can search for the requested manufacturers ID would be greatly appreciated. Thanks!

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Your line numbers don't match but I think you have problems with these lines:
    Code:
    const int IDLEN=30; //All constant values defined
    const int POLARITYLEN=3;
    const int MAXSTOCKITEMS=10;
    //First structure defined
    struct TransistorRec {
        char manufacturersID[IDLEN];
        char polarity[POLARITYLEN];
    Don't you get an error here?

    Code:
    TransistorRec a[10];    // missing "struct" 
    ...
    int index = linearSearch ( struct TransistorRec ID[], ManIDS, total);  
    // You have to call linearSearch() with the actual array (a)
    ...
    printf("String %s found at index %d\n", manufacturersID[index], index);
    // "manufacturersID" is a member of a struct, thus you can't use it without a leading struct object
    // You used it correctly a few lines above and inside linearSearch()
    Bye, Andreas

  3. #3
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    if (searchKey == ID[i].manufacturersID)
    You cannot compare c-strings this way.
    Use strcmp().
    Kurt

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    When passing a parameter to a function, you don't pass its type information, just it's name.
    E.g. ID in the case of line 57.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    What happens if the user enters a number larger than the max more than twice?

    Also you're not using your constant MAXSTOCKITEMS in your array declaration, which you should.
    Code:
    struct TransistorRec a[ MAXSTOCKITEMS ];

  6. #6
    Registered User
    Join Date
    Apr 2013
    Posts
    11
    Thanks for all the help so far! I am still a bit unsure of how to call my function. I have changed Linear search to the following:
    Code:
    #include "stdio.h"
    
    
    int linearSearch (struct TransistorRec ID[], const char *searchKey, int arraySize) {
        for (int i = 0; i < arraySize; ++i) {
            if (strcmp(ID[i],searchKey)==0)
                return i;
        }
    
    
        // We didn't find the searchKey in the Array
        return -1;
    }
    and then call the function with this
    Code:
    int index = linearSearch (a[].manufacturersID, ManIDS, total);
    This is where I am getting an error saying "expected primary-expression before ']' token"

    Thanks in advance!

  7. #7
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You want to pass an array of struct TransistorRec to that function.
    that would be
    Code:
    int index = linearSearch ( a , ManIDS, total);
    Kurt

  8. #8
    Registered User
    Join Date
    Apr 2013
    Posts
    11
    Thanks, but now doing that I get errors saying "invalid conversion from 'char' to 'const char*' As well as 'initializing argument 2 of 'int linearSearch(TransistorRec*, const char*, int)' '
    any suggestions?

  9. #9
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    ManIDS is currently defined to be a single character.
    It is supposed to be an array of characters
    try
    Code:
    char ManIDS[IDLEN];
    Kurt

  10. #10
    young grasshopper jwroblewski44's Avatar
    Join Date
    May 2012
    Location
    Where the sidewalk ends
    Posts
    294
    As well as 'initializing argument 2 of 'int linearSearch(TransistorRec*, const char*, int)' '
    To me, this means that you are passing the second argument of the function without initializing it.

  11. #11
    Registered User
    Join Date
    Apr 2013
    Posts
    11
    Quote Originally Posted by ZuK View Post
    ManIDS is currently defined to be a single character.
    It is supposed to be an array of characters
    try
    Code:
    char ManIDS[IDLEN];
    Kurt
    Changing ManIDS to an array as suggested, returns a list of errors in my function.
    All errors come from the following line:
    -int linearSearch (ID, const char *searchKey, int arraySize)

    The list of errors are as follows:
    - ID was not declared in this scope
    - expected primary expression before "const"
    -expected primary expression before "int"
    - initializer list treated as compound expression
    - expected ',' or ';' before '{' token

    Also at the moment I am not telling my function to look into a.manufacturersID, just a, could that be a source of error.

    Thanks again for the help provided!

  12. #12
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    Quote Originally Posted by thekid View Post
    Changing ManIDS to an array as suggested, returns a list of errors in my function.
    All errors come from the following line:
    -int linearSearch (ID, const char *searchKey, int arraySize)
    Now you have to explain to us, how changing ManIDS also changed the function prototype.

    Bye, Andreas

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing an Array of Struct type in Function
    By Euph11 in forum C++ Programming
    Replies: 4
    Last Post: 02-16-2013, 04:52 PM
  2. Difference Between A Linear Search And Binary Search
    By ImBack92 in forum C Programming
    Replies: 4
    Last Post: 05-12-2011, 08:47 AM
  3. Linear Search and Struct
    By Disident in forum C Programming
    Replies: 4
    Last Post: 06-22-2009, 12:06 PM
  4. binary search or linear search
    By vajira in forum C Programming
    Replies: 0
    Last Post: 06-05-2003, 12:42 PM

Tags for this Thread