Thread: Struc and Switch case

  1. #31
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Here's what I got so far... but for some reason it ain't really doing what i wanted it to.. haa

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    struct month{
    	char monthEng[10];
    	char monthSpan[11];
    	int orders;
    	int numberDays;
    };
    
    // put your function declarations here
    int find_day(struct month *m, char *user, int day);
    void intoStruct(struct month m[]);
    
    
    int main(void) {
    	struct month months[12];
    	intoStruct(months);
    	int enter_day;
    	char enter_month;
    	int monthLeng[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    	const char monthschar[12][10] = { "january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december" };
    	int i=0;
    
            // code starts here for me.
    	printf("What month: "); //asking for month
    	fflush(stdout);
    	scanf("&#37;s", &enter_month);
    	printf("What day: "); //asking for day
    	fflush(stdout);
    	scanf("%d", &enter_day);
    
    	if(enter_day > 31 || enter_day < 1)
    	{
    		printf("The entered day of month does not exist. Which was %d", enter_day);
    		fflush(stdout);
    	}
    	// Checking to see if the enter_month is == to english or spanish
    	for(i = 0; i < 12; i++)
    	{
    		if(strcmp(enter_month, monthschar[i]) == 0)
    		{
    			strcpy(monthschar[i], enter_month);
    			printf("The month %s", enter_month);
    			fflush(stdout);
    		}
    		else if(strcmp(enter_month, monthschar[i]) == 0)
    		{
    			printf("The month %s", enter_month);
    		}
    		else
    		{
    			printf("That month cannot be found either in english or in spanish, please check your spelling.");
    			fflush(stdout);
    		}
    	}
    	// Calling the functions
    
    	int findDayCount(m, enter_month, enter_day);
    
    
    
    
    	/* English to Spanish
    	 *
    	 */
    
    
    
    
    
    
    
    	return EXIT_SUCCESS;
    }
    
    int find_day(struct month *m, char *user, int day){
    
    }
    
    void intoStruct(struct month m[]){
    
    }
    So wrote some other declaration functions just in case, i had a feeling I would need them to get the months from the array. Been working on this, debugging it and stuff... And I still can't get it to work. I was talking about this with my friend and he told me, instead of telling the day of the month, why don't you tell the day of the month as well the day of the year. So January 31st will be the 31st day of the year, 31/365 (no leap year included). So I was hmm, thats interesting, that might help me one day. So i tried to wrap my head around it, and its hard haha.

    can anyone provide any advice for me?

  2. #32
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Actually, if I remember correctly, that is similar to one of the challenge questions for the Waterloo Canadian Computing Compitition a few years back.

    Anyhow, when I complie this with gcc, I get

    Code:
    datesmonths.c: In function 'main':
    datesmonths.c:43: warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast
    datesmonths.c:45: warning: passing argument 1 of 'strcpy' discards qualifiers from pointer target type
    datesmonths.c:45: warning: passing argument 2 of 'strcpy' makes pointer from integer without a cast
    datesmonths.c:49: warning: passing argument 1 of 'strcmp' makes pointer from integer without a cast
    datesmonths.c:61: warning: parameter names (without types) in function declaration
    First of all, you can flush stdout by printing a newline. You don't need those calls to fflush() unless you really want to have the user enter the data on the same line as the prompt.

    I'll take another look. see what else I can spot. I don't think that loop looks exactly correct to me . . .
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  3. #33
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    First off, enter_month should be a string (or array):

    Code:
    char enter_month[128];
    (Note that I just use 128 chars as a maximum input size. It's my standard string size, you may want to change that.)

    Next, you may want to change

    Code:
    printf("What month: "); //asking for month
    fflush(stdout);
    scanf("%s", &enter_month);
    Into something more like
    Code:
    printf("What month?\n");
    fgets(enter_month, sizeof(enter_month), stdin);
    enter_month[strlen(enter_month)-1] = 0; /* Strip the newline from the string */
    Always a good idea to use fgets() when dealing with strings, since it supports entering strings with spaces in them . . .

    Now, the loop. Here's your original:

    Code:
    for(i = 0; i < 12; i++)
        {
            if(strcmp(enter_month, monthschar[i]) == 0)
            {
                strcpy(monthschar[i], enter_month);
                printf("The month %s", enter_month);
                fflush(stdout);
            }
            else if(strcmp(enter_month, monthschar[i]) == 0)
            {
                printf("The month %s", enter_month);
            }
            else
            {
                printf("That month cannot be found either in english or in spanish, please check your spelling.");
                fflush(stdout);
            }
        }
    Almost. Very close -- but ask yourself, what happens once a month is found? Does the loop continue? Do you want it to print "That month . . ." every time it fails to match a month?

    And the rest, I see, you haven't done much on. Probably because the loop isn't working.

    So, you may want a loop something like so . . .

    Code:
    int found = 0;
    int i;
    for(i = 0; i < 12; i ++) {
        if(strcmp(month_entered, monthschar[i]) == 0) {
            printf("Month found! Its index is %i\n", i);
            found = 1;
            break;
    }
    if(found == 0) printf("Month was not found . . . \n"); /* Loop back to the month entering code here */
    Or something like that.
    Do as I say, not as I do . . .

    Experimentation is the essence of programming. Just remember to make a backup first.

    "I'm a firm believer that <SomeGod> gave us two ears and one mouth for a reason - we are supposed to listen, twice as much as we talk." - LEAF

    Questions posted by these guidelines are more likely to be answered.

    Debian GNU/Linux user, with the awesome window manager, the git version control system, and the cmake buildsystem generator.

  4. #34
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    OoO nightowl, thanks! Here is another thread that I posted. Its a updated version of my code.

    http://cboard.cprogramming.com/showt...d=1#post810061

    thats the link. I'll update what I have on it. Might be a little messy.

Popular pages Recent additions subscribe to a feed