Thread: Can anyone please help me with my code?

  1. #16
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Quote Originally Posted by matsp View Post
    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
    I'm sorry to hear about your cold. I hope you get better.

    here is my latest code for the "for loop"
    Code:
    for(i = 0; i < 12; i++)
    	{
    		if(strcasecmp(enter_month, monthschar[i]) == 0)
    		{
    			strcpy(monthschar[i], enter_month);
    			{
    				/* check if the days in the month match
    				 * if day that was entered is 31, then the months should ONLY be
    				 * january, march, may, july, august, october, december.
    				 * then it will call the function find_the_day
    				 * otherwise it will go continue on with the program.
    				 */
    			}
    			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);
    		}
    		return -1;
    	}

  2. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You are way behind me. Have you fixed the "enter_month" variable to hold at least 10 characters?

    --
    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. #18
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Quote Originally Posted by matsp View Post
    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
    OoO that fixed it... I went back to type where i declared the type. and it changed it from
    Code:
    char enter_month;
    to...
    Code:
    char enter_month[12];
    Thank you for fixing that for me.

    but
    Code:
    strcpy(monthschar[i], enter_month);
    still says that i have a warning. And the warning says "passing argument 1 from "strcpy" discards qualifiers from pointer target type"

    Is it because I'm trying to copy my enter_month into a string, so should I use &enter_month?

  4. #19
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dlwlsdn View Post
    Code:
    strcpy(monthschar[i], enter_month);
    still says that i have a warning. And the warning says "passing argument 1 from "strcpy" discards qualifiers from pointer target type"

    Is it because I'm trying to copy my enter_month into a string, so should I use &enter_month?
    No, it's telling you that "You told me you weren't going to modify the monthschar array, but now you are passing it to a function that doesn't guarantee that it's not going to change it" (and if strcpy does what it says on the tin, it SHOULD modify the destination variable!). Why do you want to store what the user entered into the array of month names?

    --
    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. #20
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    okay i fixed that problem too, i switch it from
    Code:
    strcpy(monthschar[i], enter_month);
    to....
    Code:
    strcpy(enter_month, monthschar[i]);
    reading over and over again, something sounded wrong. and Now it says copy monthschar[i], into enter_month. That way it will be from the array weee Thank you so much. Hmm.. but there is still a problem that I'm facing. or a dilemma. And that is to make the days in the month matching the month. Meaning that feburary has 28 days (29 including leap year), but if someone typed in 31, it should leave all the months that don't have 31 days in it.

  6. #21
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    I was thinking about writing a if statement in the strcpy.

  7. #22
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    hey matsp, I have favor to ask of you. I have a feeling I won't be done by the night. So i was wondering if you can watch this forum, for more posts from me? After like I update my code and if I run into a problem? If not, its totally fine, you've helped me solve like the problem I've been having for the past like 2 hours so. hehe, at the moment your like a life saver. ^^

  8. #23
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    i was wondering about my code. I realized I haven't been using my struct names. Like monthEng and monthSpan. Now I'm at the point of confusion because I wanted to learn how to use the struct members. haha.

  9. #24
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    To use an element in a structure, you use the following:

    Code:
    structname.elementname
    So it's the name of the structure, then a fullstop, then the name of the element in the structure. Here's an example:

    Code:
    printf("Andrew's age is &#37;d.",andrew.age);
    In the above example, the structure is called 'andrew', and the element in the structure is 'age'.

  10. #25
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    So I got pretty far in the past... many hours haha. But this what I got, but I'm still having some trouble. There are 3 things going wrong. If the month is a correct month in spanish or in english, it should return -1, and if the day is out of bounds, then it should return -1 as well. And then, if they wrote like february 28, then it will come out as 31 + 28 = 59 day of the month. I want to be able to do that, how do I do that? If your curious, here's my code... its quite long so.. please, if you want to post a shorter one, let me know.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    
    struct month{
    	char nameEng[10];
    	char nameSpan[11];
    	int order;
    	int numDays;
    };
    
    // put your function declarations here
    int findDayCount(struct month *m, char *user, int day);
    void initStruct(struct month month[]);
    
    
    int main(void) {
    	// declare other variables here
    	struct month months[12];
    	initStruct(months);
    	int enter_day;
    	char enter_month[11];
    	int x;
    
    	// lots of your code here
    	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);
    
    
    	x = findDayCount(months, enter_month, enter_day);
    	printf("That day is the #%d in the entire year out 365 days.", x);
    
    	return EXIT_SUCCESS;
    }
    
    int findDayCount(struct month *m, char *user, int day){
    	int total=0;
    	int i;
    	// Checking to see if the enter_month is == to english or spanish
    	for(i = 0; i < 12; i++)
    	{
    		if(strcasecmp(user, m[i].nameEng) == 0)
    		{
    			strcpy(user, m[i].nameEng);
    			printf("You entered in the month, %s, in english.\n", user);
    			fflush(stdout);
    		}
    		else if(strcasecmp(user, m[i].nameSpan) == 0)
    		{
    			strcpy(user, m[i].nameSpan);
    			printf("You enterd in the mont, %s, in spanish.\n", user);
    			fflush(stdout);
    		}
    		else
    		{
    			printf("That month cannot be found either in english or in spanish, please check your spelling.\n");
    			fflush(stdout);
    			return -1;
    		}
    	}
    	// Checking to see if the days are in the right range.
    	if(day > 31 || day < 1)
    	{
    		printf("There are no %d days in that month.\n", day);
    		fflush(stdout);
    	}
    	else if(day > 28 && (user == "february" || user == "febrero"))
    	{
    		printf("There are no %d in the month, %s. There is only 28.\n", day, user);
    		fflush(stdout);
    	}
    	else if(day > 30 && (user == "april" || user == "june" || user == "august" || user == "september" || user == "november"))
    	{
    		printf("There are no %d in the month, %s. There are only 30 days.", day, user);
    		fflush(stdout);
    	}
    	else if(day < 1)
    	{
    		printf("There are is no zero day. Did you type in the day correctly?\n");
    		fflush(stdout);
    	}
    	total = m[i].numDays + day;
    	return(total);
    }
    
    
    void initStruct(struct month month[]){
    	int monthLeng[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    	strcpy(month[0].nameEng, "January");
    	strcpy(month[0].nameSpan, "Enero");
    	month[0].numDays = monthLeng[0];
    	month[0].order = 1;
    
    	strcpy(month[1].nameEng, "February");
    	strcpy(month[2].nameSpan, "Febrero");
    	month[1].numDays = monthLeng[1];
    	month[1].order = 2;
    
    	strcpy(month[2].nameEng, "March");
    	strcpy(month[2].nameSpan, "Marzo");
    	month[2].numDays = monthLeng[2];
    	month[2].order = 3;
    
    	strcpy(month[3].nameEng, "April");
    	strcpy(month[3].nameSpan, "Abril");
    	month[3].numDays = monthLeng[3];
    	month[3].order = 4;
    
    	strcpy(month[4].nameEng, "May");
    	strcpy(month[4].nameSpan, "Mayo");
    	month[4].numDays = monthLeng[4];
    	month[4].order = 5;
    
    	strcpy(month[5].nameEng, "June");
    	strcpy(month[5].nameSpan, "Junio");
    	month[5].numDays = monthLeng[5];
    	month[5].order = 6;
    
    	strcpy(month[6].nameEng, "July");
    	strcpy(month[6].nameSpan, "Julio");
    	month[6].numDays = monthLeng[6];
    	month[6].order = 7;
    
    	strcpy(month[7].nameEng, "August");
    	strcpy(month[7].nameSpan, "Agosto");
    	month[7].numDays = monthLeng[7];
    	month[7].order = 8;
    
    	strcpy(month[8].nameEng, "September");
    	strcpy(month[8].nameSpan, "Septiembre");
    	month[8].numDays = monthLeng[8];
    	month[8].order = 9;
    
    	strcpy(month[9].nameEng, "Octber");
    	strcpy(month[9].nameSpan, "Octubre");
    	month[9].numDays = monthLeng[9];
    	month[9].order = 10;
    
    	strcpy(month[10].nameEng, "November");
    	strcpy(month[10].nameSpan, "Noviembre");
    	month[10].numDays = monthLeng[10];
    	month[10].order = 11;
    
    	strcpy(month[11].nameEng, "December");
    	strcpy(month[11].nameSpan, "Diciembre");
    	month[11].numDays = monthLeng[11];
    	month[11].order = 12;
    
    }

  11. #26
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > printf("That month cannot be found either in english or in spanish, please check your spelling.\n");
    You need to do this outside the for loop, and then only if you fail to find a month which matches.
    Set a "found" flag inside the loop, which you test outside.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  12. #27
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The first for loop in findDayCount is problematic, because in the first instance, if the month isn't January or Enero, you will get "That month cannot be found...".

    You can use a boolean variable here to check for a true or false situation:
    Code:
    int bool=0;
    for(i = 0; i < 12; i++)
    	{
    		if(strcasecmp(user, m[i].nameEng) == 0)
    		{
    			strcpy(user, m[i].nameEng);
    			printf("You entered in the month, &#37;s, in english.\n", user);
    			fflush(stdout);
    			bool=1; break;
    		}
    		else if(strcasecmp(user, m[i].nameSpan) == 0)
    		{
    			strcpy(user, m[i].nameSpan);
    			printf("You enterd in the mont, %s, in spanish.\n", user);
    			fflush(stdout);
    			bool=1; break;
    		}
    	}
    if (bool==0)
    		{
    			printf("That month cannot be found either in english or in spanish, please check your spelling.\n");
    			fflush(stdout);
    			return -1;
    		}
    The reason I added break is because once the month has been matched you're finished in the loop.

    ps. I am sure this is what Salem means by using a "found" flag, and in light of the fact s/he dwells at The Edge of the Known Universe, let's call this using a flag and not a "boolean variable", which an int we know is not TRUE-ly such a thing anyway
    Last edited by MK27; 11-23-2008 at 02:22 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  13. #28
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    hmm, makes sense. I'll try it out. Thanks for the quick response. I'll let you guys know how it goes after this is done. Thanks.

  14. #29
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Quote Originally Posted by MK27 View Post
    The first for loop in findDayCount is problematic, because in the first instance, if the month isn't January or Enero, you will get "That month cannot be found...".

    You can use a boolean variable here to check for a true or false situation:
    Code:
    int bool=0;
    for(i = 0; i < 12; i++)
    	{
    		if(strcasecmp(user, m[i].nameEng) == 0)
    		{
    			strcpy(user, m[i].nameEng);
    			printf("You entered in the month, %s, in english.\n", user);
    			fflush(stdout);
    			bool=1; break;
    		}
    		else if(strcasecmp(user, m[i].nameSpan) == 0)
    		{
    			strcpy(user, m[i].nameSpan);
    			printf("You enterd in the mont, %s, in spanish.\n", user);
    			fflush(stdout);
    			bool=1; break;
    		}
    	}
    if (bool==0)
    		{
    			printf("That month cannot be found either in english or in spanish, please check your spelling.\n");
    			fflush(stdout);
    			return -1;
    		}
    The reason I added break is because once the month has been matched you're finished in the loop.

    ps. I am sure this is what Salem means by using a "found" flag, and in light of the fact s/he dwells at The Edge of the Known Universe, let's call this using a flag and not a "boolean variable", which an int we know is not TRUE-ly such a thing anyway
    So this works awesome like magic. But there is something still going on here. That I can't seem to go around. When I type in february, it says that it can't be found. But i put it in my function down below. So what is happening? I tried debugging it, going step-by-step, but I still can't seem to find out whats wrong. Do you guys have any idea? hah, been trying to get this to work. Awesome thing is that I'm learning a lot just by making small mistakes. And its thanks you guys that are being very helpful that I can actually understand whats happening. Thanks again.

  15. #30
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by dlwlsdn View Post
    So this works awesome like magic. But there is something still going on here. That I can't seem to go around. When I type in february, it says that it can't be found.
    What about "February"?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

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, 04: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, 06:05 PM