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!