Can someone guide me how can I use scanf for a structure?

const int IDLEN = 30;
const int POLARITYLEN = 3;
const int MAXSTOCKITEMS = 10;

struct TransistorRec {
	char manufacturersID[IDLEN];
	char polarity[POLARITYLEN];
	float power;
	float gain;
	int stock;
};

typedef struct TransistorRec Transistor;

struct StockRec{
	int size;
	Transistor stocklist[MAXSTOCKITEMS];
};

typedef struct StockRec Stock;


#include "stdio.h"


int main()

{
	printf("Enter manufacturer's ID, polarity, power, gain, stock ");
	scanf("%c %c %f %f %i",TransistorRec.manufacturersID[0],TransistorRec.polarity[0],TransistorRec.power[0],TransistorRec.gain[0],TransistorRec.stock[0]);

return 0;

}

Hey definition of a struct doesn't create struct objects for you to play around as variables of data type struct.

You need to create the objects of that struct to access memory and do other assignment stuff. So try this:

int main()
{
TransistorRec a;
printf("Enter manufacturer's ID, polarity, power, gain, stock ");

//And access the object as follows...

scanf("%c %c %f %f %i",a.manufacturersID[0],a.polarity[0],a.power[0],a.gain[0],a.stock[0]);

I hope you got the difference between defining a struct and creating an object of that struct type. One thing to know is only objects are allocated memory and not the definition of struct. And so only objects come in handy.Enjoy coding... :)

csurfer is completely right!

look at this too...

scanf("%s %s %d  %d %i", a.manufacturersID, a.polarity, &a.power, &a.gain, &a.stock);

- First: The scanf function receive just memory adress of the variables (you need to use & operator).

- Second: manufacturersID and polarity are arrays of caracters. Its names are already pointers to their first element. So you dont need to use & operator or [] (indexer).

Obs.: Sorry about the poor english ;)

What if I need to scanf a few times? How do I store those variables in an array and access them later?

int main()

{
	int total,i;

	

	printf("How many transistor in stock?  ");
	scanf("%i",&total);

	for ( i = 0; i <total; i++){
		printf("Enter manufacturer's ID, polarity, power, gain, stock :\n");
	
	
	TransistorRec a;
	scanf("%c %c %f %f %i",a.manufacturersID,a.polarity,&a.power,&a.gain,&a.stock);
	}


	return 0;
}

exactly like you would any array of objects, such as an array of its. Note that the first two parameters of the format string need to be %s, not %c because %c only allows entry of a single character while %s allows entry of an entire word. You should also be aware that scanf() can cause corrupt memory because it does not do a bounds check, such as if you are entering the manufacturersID but type more then IDLEN characters.

struct TransistorRec {
	char manufacturersID[IDLEN];
	char polarity[POLARITYLEN];
	float power;
	float gain;
	int stock;
};

int main()

{
	int total,i;
	TransistorRec a[10];

	

	printf("How many transistor in stock?  ");
	scanf("%i",&total);

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

	return 0;
}

Hi guys
When I run the last code posted above, it states that printf and scanf identifier not found. What does that mean exactly?
Thanks

simply needed #include <stdio.h>......

So the above code works perfectly. Now I want to print the number of different transistors in stock, as well as manufacturors ID, polarity, maximum power, current gain, number in stock, all in tabular format, how do I go about that?
Something like:
printf(%s%s%s%s%s%s\n,"Number of Transistors", "ManID", "Polarity", "Max Power", "Current Gain", "stock");
Wont this only print the titles though? How do I print the actual data associated with each heading?
Thanks in advance

depends on how you declared data that contains the individual items. Something like this:
.

int nTransistors;
char ManID[20];
char Polarity[20];
// populate the above with info now shown here
printf("%d %s %s\n", nTransistors, ManID, Polarity);

Ancient Dragon, I am using the code exactly as you have written it 5 posts up from this one. That is what I am running. So what Id like to do is now before the program terminates is to print all the data inputted by the user on the screen. How do I printf a struct?

When I use the suggested code to print the inout data, it tells me that "int Transistors" was not initialized. This means that it wasnt declared in the "int main()"?

would it help if i say this function receives as a parameter an array of structures containing the transistor details. So I suppose I have to create an array for each input entry?

When I use the suggested code to print the inout data, it tells me that "int Transistors" was not initialized. This means that it wasnt declared in the "int main()"?

are you using exactly the code posted by AD cause there's no variable Transistors found there

Ancient Dragon, I am using the code exactly as you have written it 5 posts up from this one. That is what I am running. So what Id like to do is now before the program terminates is to print all the data inputted by the user on the screen. How do I printf a struct?

almost the same way as you used scanf on the contents, use a loop to traverse and print the elements of the struct array

Zeroliken, I am using the code that AD posted as well as

int nTransistors;
char ManID[20];
char Polarity[20];
// populate the above with info now shown here
printf("%d %s %s\n", nTransistors, ManID, Polarity);

it then states that nTransistors was not initialized. Can I simply add this extra code in or do I define nTransistors somewhere else as well?

where exactly did you put those variables?...
if it's in the struct printing it (after populating it with data) is something like
printf("%d %s %s\n", a[i].nTransistors, a[i].ManID, a[i].Polarity);
where i is the current index of a loop if your using an array of struct

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.