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; Nope, it just keeps returning that it doesn't exist and -1. Which somewhat good, because we know that loop works. ...

  1. #31
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Nope, it just keeps returning that it doesn't exist and -1. Which somewhat good, because we know that loop works. But I think something is happening between the two functions of initStruct and findDayCount. I'm still looking at it, can't seem to find it.

  2. #32
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I notice that there is this:

    Code:
    strcpy(month[1].nameEng, "February");
    	strcpy(month[2].nameSpan, "Febrero");
    	month[1].numDays = monthLeng[1];
    	month[1].order = 2;
    Which would certainly screw up febrero.
    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

  3. #33
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    OoO nice!

    you know in the function it says

    Code:
    struct month months[12];
    initStruct(months);
    /*.....
    *
    *
    */
    x = findDayCount(months, enter_month, enter_day);
    could it be that its saying that it is saying the months[12], would be back at january? Thats what I think it is, if so. how would i change the initStruct or fix this problem?

  4. #34
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by dlwlsdn View Post
    could it be that its saying that it is saying the months[12], would be back at january?
    No, it's the same as any other array, eg, foo[10] has ten elements, 0-9. There is no months[12], and i<12 so that never happens.
    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

  5. #35
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    oh, okay. So where is the problem. I'm still looking for it haha. Could it be the way I'm calling my findDayCount function is wrong?

  6. #36
    Registered User
    Join Date
    Sep 2008
    Posts
    10
    What exactly is it your trying to do?

    I just wrote a program that has the user input a month, either in English or Spanish, and then translates it and gives the number of days in that month.

    I could post it for reference?

  7. #37
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Quote Originally Posted by mmcg View Post
    What exactly is it your trying to do?

    I just wrote a program that has the user input a month, either in English or Spanish, and then translates it and gives the number of days in that month.

    I could post it for reference?
    Yeah, something like that.
    The user will input a month and a day. So .i.e. February 28. Then the outcome should be that February 28 is the 59th day in the year.

    something like this.

  8. #38
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    First: Can I just point out that using a name that is a predefined type in C++ is not a great idea, so calling the variable something OTHER than "bool" is probably a good idea. "found" perhaps?

    Second:
    Code:
    	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);
    	}
    wasn't the whole idea of having a struct months[12] that you don't have to mess about recognising names[which by the way, you are doing in the wrong way], but rather can use the months struct to find out if the day is outside the appropriate range.

    --
    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. #39
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Hmm. I understand. Still working on it. Not giving up haha . Thanks for helping and giving me advice

  10. #40
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    Okay.... I really can't find out whats wrong with my findDayCount function..
    Code:
    int findDayCount(struct month *m, char *user, int day){
    	int total=0;
    	int i;
    	int test=0;
    	// 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, &#37;s, in english.\n", user);
    			fflush(stdout);
    			test = 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);
    			test = 1; break;
    		}
    		if(test == 0)
    		{
    			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);
    }
    it still tells me that I'm typing in the months wrong, except for january. Like january works fine.
    and the "total" comes out to be -1 or some weird number haha. RAWR~ lol

  11. #41
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Put "if (test == 0) ..." outside of the for-loop!

    Also:
    Code:
    (user == "february")
    tests if the address of user is the same address as the constant string "february". That is (nearly) impossible, as one is a local variable, and the other is a constant string value.

    --
    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.

  12. #42
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    matsp, you are a genius. How come I didn't see that before? I runs perfectly, except one thing... it didn't add the numbers correctly. I typed in february 28. And it printed out
    Code:
    What month: february
    What day: 28
    You entered in the month, February, in english.
    That day is the #28 in the entire year out 365 days.
    Obviously its not the #28 out of 365, it should be 59.

    my code for it is.
    Code:
    total = m[i].numDays + day;
    	return(total);
    I know its not the right way to do it. I can't seem to find a way to accumulate every month and not accumulate january if i type in january 1 or something. This part is the last part I'm stuck on.

  13. #43
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    One way to do it . . .

    Code:
    int monthnumber;
    int months[12] = {31, 28, . . .}; /* don't feel like typing the entire array */
    int day;
    for(i = 0; i < monthnumber; i ++) {
         day += months[i];
    }
    day += enteredday;
    That should do it. (Something like that, anyhow . . .)
    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.

  14. #44
    Resu Deretsiger Nightowl's Avatar
    Join Date
    Nov 2008
    Location
    /dev/null
    Posts
    186
    Gahh, day should be initialized to zero. Otherwise you'll get some pretty strange results.
    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.

  15. #45
    Registered User
    Join Date
    Oct 2008
    Posts
    91
    OoO interesting. Nice night owl.. month number being what the user wrote. So january would be 1, feb. = 2, etc.

Page 3 of 4 FirstFirst 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, 09:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08: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, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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