Here is the instruction to the assignment: http://dl.dropbox.com/u/43732846/Instruction.txt

I think I got up to the part to initialize array and set length to 0 but I'm not sure how to do a for loop to print.

I tried like strlen(month), printf(month), etc, but none of them seems to work.

And is it supposed to be 'length int' under word_length structure? Or is this a mistake since int is a data type and cannot be used as a variable??

Could you help me out?

Thanks!


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define	 MONTHS			 12
#define  LENGTH			 1

struct word_length {
	char *word;
	int count;
} month[] = {
	{ "January", 0 },
	{ "February", 0 },
	{ "March", 0 },
	{ "April", 0 },
	{ "May", 0 },
	{ "June", 0 },
	{ "July", 0 },
	{ "August", 0 },
	{ "September", 0 },
	{ "October", 0 },
	{ "November", 0 },
	{ "December", 0 }
} ;

int main() {
	int i = 0;
	for (i = 0; i < 12; i++) {
		strlen(words);
	}

	printf("\n\n\nPress any key to continue.");
	getchar(); getchar();
	exit(0);
}

Recommended Answers

All 3 Replies

You don't need to initialize the array at first.

But you should define an array after

int main(void) // use void, don't leave it blank
// declare your array to your previously defined struct here.
// I'm not going to do it for you.
int i ;

for (i = 0; i <12; i++)
{
     // Fill in the values for your *struct here.
}

That should get you going along the right direction. If I would do more, I should probably sign up for your class and receive credit.

commented: Thank you! +2

I got it to print name of the months and length, but length says 0 for all months. I think I'm supposed to use strlen but I get error for all the combinations I have tried to print using strlen....

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define	 MONTHS			 12
#define  LENGTH			 1

struct word_length {
	char *word;
	int count;
} ;

int main(void) {

	word_length month[] = {
		{ "January", 0 },
		{ "February", 0 },
		{ "March", 0 },
		{ "April", 0 },
		{ "May", 0 },
		{ "June", 0 },
		{ "July", 0 },
		{ "August", 0 },
		{ "September", 0 },
		{ "October", 0 },
		{ "November", 0 },
		{ "December", 0 }
	} ;

	int i;
	for (i = 0; i < 12; i++) {
		// strlen(month[i].count);
		printf("%s \t %i \n", month[i].word, month[i].count);
	}

	printf("\n\n\nPress any key to continue.");
	getchar(); getchar();
	exit(0);
}

No, you don't understand.
You've already defined a struct at the beginning. You need to declare an array to the struct you've declared.

struct word_length MyWord[MONTHS] ; 
// And then you need to fill in the values in your for loop
MyWord->word = "January" ;
MyWord->count = strlen("January") ;

This is just one way to do what you want. There are other ways. You should read up on how to declare structs and how to use them. Based on the posts you've been listing here, you haven't done your homework. And that is My Word for the day

You can use your way. But I believe your instructor wants you to use the strlen function. You're not doing that. What is the length of "January"? It's certainly not 0. Be sure to read your instructor's instructions carefully. Otherwise, everything may come out correctly. But since you have not been careful to follow instructions, you may not get the grade you want.

You need to fill in the values in the manner he requests.

Then within for-loop, find the length of each month using strlen from string library,
and update the array.

I don't believe you will pass this part of the assignment.

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.