Can anyone please help me with my code?

This is a discussion on Can anyone please help me with my code? within the C Programming forums, part of the General Programming Boards category; Code: #include <stdio.h> #include <stdlib.h> #include <string.h> struct month{ char monthEng[10]; char monthSpan[11]; int orders; int numberDays; }; // put ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    91

    Can anyone please help me with my code?

    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("%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 what i'm trying to do is type in a month, i.e. january, and then make sure they typed in write, but then i gotta see if they typed it in spanish and see if they got it write, but if it typed it in a totally weird word like "NoMonth", then it will say "There is no month by "NoMonth" in English or in Spanish." or smething like that.... then my friend proposed that i should also say something... "January 31st the 31st day of the whole year.".... I dunno, been working on this for a bit, but I can't get around it. Can anyone help me?

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So which exact bits is it that you can not solve?

    The more specific you are, the easier it is for us to help you with the specific problem you are having, rather than trying to guess the part you are struggling with.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Sorry, haha I'm just freakiing out haha. I gotta calm down. Well the first part is that. I realized that it might be easier if i made a declare function so i named it intoStruct(struct month *m), where it can hold the months easier. Then find_the_day function is to find the day of the whole year.

    But what I'm really struggling on is my main body. I can seem to compare the two strings. Month from user and month in the code. I thought my code seemed write, but something terrible happens. It doesn't return what i want it to. Also i'm trying to make sure that if they type over 31 for days, it will say there are no 31 days for that month, or for feburary, there is no 30 days in that month, etc. I dunno, I'm at the point of confusion, but thanks for replying very quickly, its really calming me down and relaxing me. Just knowing that there are people that are very helpful. Thanks you very much.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I don't see anything immediately wrong with your strcmp() calls, so perhaps you can describe the difference between what you expect, and what you actually get.

    Let's solve one problem at a time - it is often very easy to get carried away and try to write too much code at once.

    By the way, do you know the different between the word WRITE and RIGHT? Sorry to be pedantic, but it's actually quite confusing when people use a word that sounds the same but spelled differently [and I do apologize profoundly if it happens to be that you are using a speech-to-type machine because you are somehow unable to use your hands to type].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Okay! So, in the main I got this
    Code:
    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);
    
    	return EXIT_SUCCESS;
    }
    When i run the program it asks me to type in a month and I type in "JAnUary", and it asks me to type in the day, so I type in 31. Since "JAnUary" is not the same as "january", I wanted it to tell me that there is smething wrong with what I typed in, and if was equal to each other, then I would want it to say that I typed in the month "january" and the day. But for some reason, it doesn't even do that.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    Use the tolower() function on the string before you compare it, that way whatever case the user enters it in, it'll still compare properly.

    And can structures be passed to a function? I don't think they can.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    I'm still working on it, just reading it line by line and trying to see what happens, or what goes wrong. I am very grateful that you are offering your help to me. I really appreciate it.

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    See what I mean about being specific? If you had described that in the first place, it would have been easy to answer.

    If your compiler (actually the C runtime) supports it, stricmp or strcasecmp will compare strings regardless of case. If you can't use stricmp or strcasecmp, then you need to convert each letter in the string to lowercase with tolower, then compare the string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    Is it just me or are the if and elseif comparisons in the for loop exactly the same?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by mmcg View Post
    Is it just me or are the if and elseif comparisons in the for loop exactly the same?
    Yes, I think so to (at least in essense) - I think it's meant to be one English and one Spanish loop.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Quote Originally Posted by mmcg View Post
    Use the tolower() function on the string before you compare it, that way whatever case the user enters it in, it'll still compare properly.

    And can structures be passed to a function? I don't think they can.
    tolower() function? Would it be something like this?
    Code:
    tolower(string1, string2); // String1 being from the user, string2 from months or...
    /*...*/
    tolower(string1, "january");
    I have a feeling that there is more to it than that, but is it something like that?

  12. #12
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Okay, everything runs smoothly all to the for loop, once it hits for loop. I think the program goes crazy, or I go crazy. lol

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    My mindreading is a bit poor today - probably the horrible cold I've got - I can't properly see the code you have. Could you perhaps post the latest version?

    And in case you haven't got that, tolower takes ONE CHARACTER and if the input is an upper case A-Z, it returns the lower-case variant (it may also do foreign alphabeticals).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Code:
    for(i = 0; i < 12; i++)
    	{
    		if(strcasecmp(enter_month, monthschar[i]) == 0)
    		{
    			strcpy(monthschar[i], enter_month);
    			printf("The month &#37;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);
    		}
    	}
    So I tried what you preferred which was strcasecmp() and it can read it. So YAY!!! I don't need to worry about capitalization and stuff Thanks to you.... but....

    I get a warning in a few places. Not sure what these errors mean. I get the warning at this part of the for loops
    Code:
    if(strcasecmp(enter_month, monthschar[i]) == 0)
    It says "passing argument 1 of "strcasecmp" makes pointer from integer without a cast".

    Another warning at
    Code:
    strcpy(monthschar[i], enter_month);
    And this one says two warnings.
    1. "passing argument 2 of "strcpy" makes pointer from integer without a cast".
    2. "passing argument 1 of "strcpy" discards qualifiers from pointer target type".

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are trying to copy the entered month into your const array.

    Code:
    char enter_month;
    is ONE character - perhaps not enough to hold all the characters of "january"?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 1 of 4 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21