if user inputs Monday and 'n' = 3, your output should be Thursday.
user input = day of the wk & number of days from now (between 1000<_ n <_10000)
they want to use a strcmp and the mod operator
Im a little lost on why...and how
if user inputs Monday and 'n' = 3, your output should be Thursday.
user input = day of the wk & number of days from now (between 1000<_ n <_10000)
they want to use a strcmp and the mod operator
Im a little lost on why...and how
Last edited by digy; 05-13-2003 at 11:07 AM.
strcmp is probably used to determine which day was entered. The modulus operator is used to wrap around an array so that if you go past the end, you start over at the beginning:
Code:#include <stdio.h> #include <string.h> #define NDAYS 7 static char *days[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday", }; static int dayValue(char *day) { int i; for (i = 0; i < NDAYS; i++) { if (strcmp(day, days[i]) == 0) break; } return i; } int main(void) { int n = 3; printf("%s\n", days[(dayValue("Monday") + n) % NDAYS]); printf("%s\n", days[(dayValue("Sunday") + n) % NDAYS]); printf("%s\n", days[(dayValue("Thursday") + n) % NDAYS]); return 0; }
p.s. What the alphabet would look like without q and r.
thank you for your quick response.
Not working for me but it's coming along.
QUESTION:
The user inputs the day (monday) then inputs a number between 1000 and 10000 days.
How would you figure out what day it is from the number they inputted?
>The user inputs the day (monday) then inputs a number between 1000 and 10000 days.
The above method should work for any number of days provided you don't take leap days into account.
p.s. What the alphabet would look like without q and r.
>Not working for me but it's coming along.
>The user inputs the day (monday) then inputs a number between 1000 and 10000 days.
strcmp is case sensitive ("monday" is not the same as "Monday").
This is a slightly modified version of Brighteyes' code to demonstrate the case sensitivity.Code:#include <stdio.h> #include <string.h> #define NDAYS 7 static const char *days[] = { "Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday", }; static int dayValue(const char *day) { int i; for ( i = 0; i < NDAYS; i++ ) { if ( strcmp(day, days[i]) == 0 ) { return i; } } return -1; } void foo(const char *day, int n) { int dow = dayValue(day); printf("%s + %d = ", day, n); if( dow >= 0) { puts(days[(dow + n) % NDAYS]); } else { puts("not found"); } } int main(void) { const char myday[] = "Friday"; int n = 1234; foo("Monday", 5); foo("monday", 5); foo("Tuesday", 9); foo(myday, n); return 0; } /* my output Monday + 5 = Saturday monday + 5 = not found Tuesday + 9 = Thursday Friday + 1234 = Sunday */
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
thank you guys.
but don't have to use functions this time
Am I the only one in doing strings that always puts them as caps? I always toupper everything thats inputted by the user. Maybe its bad programming on my end. :P
To error is human, to really foul things up requires a computer
No, Twiggy, it's not bad programming. I always use toupper() also. It's just whatever habit you get into.
Walt