Hi,


I have decalared a structure named record as given below and also created a function called read() inside the structure. The function works fine when I call it using this statement:

myRecord.read();

but I want to create one more function which can be called to initilize all the variables and arrays.

When I tried to initialize an array inside the structure, it gave an warning message message that "for loop... can't be expanded inline."

What should I do to initiliza arrays defined inside a structure?

When I write the user input to a text file, garbage values are also written. So, I want to initialize arrays before using them.

Inside the given below structure if I create a function similar to the read() one, it doesn't work and wranings come:

void init() {

int count = 0; //This initialization works fine.

for(count = 0; count < 100; count++) {

 a[count] = ' ';

}

}

Can anybody help me?

void main() {

	clear;

	typedef struct record {

		char 	q[100];
		char 	a1[100];
		char 	a2[100];
		char 	a3[100];
		char 	a4[100];


		void read() {

		       printf("\nQuestion: ");
		       gets(q);

		       printf("\nA: ");
		       gets(a1);

		       printf("\nB: ");
		       gets(a2);

		       printf("\nC: ");
		       gets(a3);

		       printf("\nD: ");
		       gets(a4);

		       //printf("\nChoice: ");
		       //scanf("%d",can);
		};


}record;


	record myRecord;
	myRecord.read();
}

Recommended Answers

All 3 Replies

What is "clear"?

Your program is not a C program. It is C++. I think it is better that this post would moved to C++ section.


Inside a class or structure (both are almost same thing in C++) you can not set member values because they are not still created.

Well, it's so simple (in C++ ;) ):

struct Record {
    char q[100];
    char a1[100];
    ...
    Record() {
        memset(q,0,sizeof q);
        memset(a1,0,sizeof a1);
        ...
    }
   void read() {
   ...
};
....
Record YourRecord; ...

You can also intialize the arrays in main but outside structure,if you don't want to use inbuilt function memset() .

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.