cant get code to work

This is a discussion on cant get code to work within the C Programming forums, part of the General Programming Boards category; Hi, i cant get this to work and i dont know why, its not becuase of the date.h... i do ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    31

    Question cant get code to work

    Hi, i cant get this to work and i dont know why, its not becuase of the date.h... i do have that file in my dir.

    Code:
    #include <stdio.h>
    #include "date.h"
    
               
       char* month_name (int month) {
               
          static char* month_names[] = {
          "January", "February", "March", "April", "May", "June",
          "July", "August", "September", "October", "November", "December"
          };
          return month_names[(month-1) % 12];
       }
    
               
       void print_day (int day, int month, int year) {
               
          static char* day_names[] = {
          "Sunday", "Monday", "Tuesday", "Wednesday",
          "Thursday", "Friday", "Saturday"
          };
          char* name = day_names[week_day(day, month, year)];
          printf("%d %s %d is a %s\n", day, month_name(month), year, name);
       }
    
               
       int main () {
               
          int day;
          int month;
          int year;
       
          getchar();
          scanf("%d %d %d", &day, &month, &year);
          print_day(day, month, year);
          return 0;
       }

  2. #2
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: cant get code to work

    Originally posted by duffy
    Hi, i cant get this to work and i dont know why, its not becuase of the date.h... i do have that file in my dir.

    Code:
    #include <stdio.h>
    #include "date.h"
    
               
       char* month_name (int month) {
               
          static char* month_names[] = {
          "January", "February", "March", "April", "May", "June",
          "July", "August", "September", "October", "November", "December"
          };
          return month_names[(month-1) % 12];
       }
    
               
       void print_day (int day, int month, int year) {
               
          static char* day_names[] = {
          "Sunday", "Monday", "Tuesday", "Wednesday",
          "Thursday", "Friday", "Saturday"
          };
          char* name = day_names[week_day(day, month, year)];
          printf("%d %s %d is a %s\n", day, month_name(month), year, name);
       }
    
               
       int main () {
               
          int day;
          int month;
          int year;
       
          getchar();
          scanf("%d %d %d", &day, &month, &year);
          print_day(day, month, year);
          return 0;
       }
    what is this function? and what is in date.h?
    hello, internet!

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    31

    i got it...thanx

    Sorry thanx for trying to help, but i got it working. Cheers.

  4. #4
    Registered User
    Join Date
    Sep 2002
    Posts
    31

    Question actually need more help!

    Sorry, i do actually need more help.. i was having trouble before because i left out a file that had to be included. I got that working now. This next question is to do with the input. At the moment the input ignores the first letter thats typed in. What i want it to do is print out an error message if anyting but the first letter is a 'd'. Any other letter i want it to throw an exception.
    So if the user enters d23 11 2002, its ok, but if the user enters say x23 11 2002 it will throw an error.

    Code:
    #include <stdio.h>
    #include "date.h"
    #include "date.c"
               
       char* month_name (int month) {
               
          static char* month_names[] = {
          "January", "February", "March", "April", "May", "June",
          "July", "August", "September", "October", "November", "December"
          };
          return month_names[(month-1) % 12];
       }
    
               
       void print_day (int day, int month, int year) {
               
          static char* day_names[] = {
          "Sunday", "Monday", "Tuesday", "Wednesday",
          "Thursday", "Friday", "Saturday"
          };
          char* name = day_names[week_day(day, month, year)];
          printf("%d %s %d is a %s\n", day, month_name(month), year, name);
       }
    
               
       int main () {
               
          int day;
          int month;
          int year;
       
          getchar();
          scanf("%d %d %d", &day, &month, &year);
          print_day(day, month, year);
          return 0;
       }
    Attached Files Attached Files

  5. #5
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680
    Just insert the d in the scanf function. And always check the return value of the scanf function:
    Code:
    if(scanf("d%d %d %d", &day, &month, &year) != 3)
    {
       /* error */
    }
    B.T.W.

    A better and safer way to do this is read the complete line with the fgets function and then use the sscanf function.

    Cheers,
    Monster

  6. #6
    Registered User
    Join Date
    Sep 2002
    Posts
    31
    Its always producing the second error. Even when you enter d 23 11 2002, it does the error. The != 4 part also doesnt work...?


    Code:
    int main () {
               
          int day;
          int month;
          int year;
          char check;
          getchar();
       
          if(scanf("%c %d %d %d", &check, &day, &month, &year) != 4)
             {
             printf("error");
             }
             
             if ( check != 'd' )
             {
                printf("error2");
             }
             else{
             
             /*	scanf("%d %d %d", &day, &month, &year);*/
                print_day(day, month, year);
                return 0;
             }
       }

  7. #7
    Registered User
    Join Date
    Sep 2002
    Posts
    31
    its ok, i forgot to remove the getchar()

    thanx

  8. #8
    moi
    moi is offline
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946
    you should not be including date.c: you should prototype all exported date.c functions in date.h, include only date.h, compile date.c seprately, and shove them both into the linker together.
    hello, internet!

  9. #9
    Registered User
    Join Date
    Sep 2002
    Posts
    31

    Question just a quick q

    Now that i have the prog working for input such as d23 11 2002
    Output: 23 November 2002 is a Saturday.
    How do i get the program to do this:

    input: d 23 11
    Output: 23 November 2002 is a Saturday
    (the prog takes the current year)

    input:d 23
    Output: 23 October 2002 is a Wednesday.
    (the prog takes the current month and current year)

    Input: d
    Output: 16 October 2002 is a Wednesday
    (the prog takes the current date).

    How do i go about doing this?

    Code:
    #include <stdio.h>
    #include "date.h"
    #include "date.c"
               
       char* month_name (int month) {
               
          static char* month_names[] = {
          "January", "February", "March", "April", "May", "June",
          "July", "August", "September", "October", "November", "December"
          };
          return month_names[(month-1) % 12];
       }
    
               
       void print_day (int day, int month, int year) {
               
          static char* day_names[] = {
          "Sunday", "Monday", "Tuesday", "Wednesday",
          "Thursday", "Friday", "Saturday"
          };
          char* name = day_names[week_day(day, month, year)];
          printf("%d %s %d is a %s\n", day, month_name(month), year, name);
       }
    
               
       int main () {
               
          int day;
          int month;
          int year;
          char check;
       
       
          if(4 != scanf("%c %d %d %d", &check, &day, &month, &year))
          {
             printf("error");
          }
       
          if ( check == 'd' )
          {
             print_day(day, month, year);
             return 0;
          }
          else{
             printf("Unknown command: %c \n", check );
          
          
          }
          /*
          else{
             printf("Unknown command: %c \n", check );
             scanf("%d %d %d", &day, &month, &year);
          
          }*/
       
       }

  10. #10
    Black Mage Extraordinaire VegasSte's Avatar
    Join Date
    Oct 2002
    Posts
    167
    I think you would have to use the time.h library(look it up) as it has allsorts of time+date based functions inside!

  11. #11
    Registered User
    Join Date
    Sep 2002
    Posts
    31

    Question im so annoying

    Hi again, sorry to be so annoying, but im having so much trouble with this task!

    I have reverted back to an old program that i was working on for this a while ago. I am now having trouble with the do_day function. I need it to assume current year, if year is ommited from input, the same with month and the same with day.

    Code:
    #include <stdio.h> 
    #include <time.h> 
    #include <sys/time.h> 
    
    /* true for leap years */ 
    
    /***************************DATE.C**************************/ 
    
    
               
       int leap_year (int year) { 
               
       
          return ((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0))); 
       } 
    /* the number of days in month of year */ 
    
    
    
    
               
       int days_in_month (int month, int year) { 
               
       
          switch (month) { 
             case 9:case 4:case 6:case 11:
                return 30; 
                break; 
             case 2:
                return leap_year(year) ? 29: 28; 
                break; 
             default:
                return 31; 
                break; 
          } 
       } 
    
    /* the weekday (0 = Sunday) of a given date 
    accurate for dates in the Gregorian calendar which began on 14 September 1752 */ 
               
       int week_day (int day, int month, int year) { 
               
       
          int d = 14; /* The modern calendar began on 14 ... */ 
          int m = 9; /* ... September ... */ int y = 1752; /* ... 1752 ... */ 
          int n = 4; /* ... which was a Thursday */ 
          while (year > y || month > m || day > d) { 
             n++; 
             day--; 
             if (day < 1) { month--; day = days_in_month(month, year); } 
             if (month < 1) { year--; month = 12; } 
          } 
          return n % 7; 
       } 
    
       struct timeval tv; 
    
    
               
       int current_day () { 
               
       
          gettimeofday(&tv, 0); 
          return localtime(&tv.tv_sec)->tm_mday; 
       } 
    
    
    
               
       int current_month () { 
               
       
          gettimeofday(&tv, 0); 
          return localtime(&tv.tv_sec)->tm_mon + 1; 
       } 
    
    
    
    
               
       int current_year () { 
               
       
          gettimeofday(&tv, 0); 
          return localtime(&tv.tv_sec)->tm_year + 1900; 
       } 
    
    /************************MY PROGRAM**********************/ 
       static char myday[7][15]= 
       {{"Sunday"}, 
        {"Monday"}, 
        {"Tuesday"}, 
        {"Wednesday"}, 
        {"Thursday"}, 
        {"Friday"}, 
        {"Saturday"} 
       }; 
       int first_day_of_month ( int year, int month ); 
    
               
       char* getday(int d, int m, int y){ 
               
          int current_day; 
          int day = first_day_of_month( 2002, m ); 
          current_day = (day + d - 1) % 7; 
          return myday[current_day]; 
       } 
    
    /***************TROUBLE HERE*************/
               
       int do_day ( char *cmd ) { 
               
       
          int day = current_day();
          int month = current_month();
          int year = current_year(); 
          int res; 
          res = sscanf( cmd, "%*c %d %d %d", &day, &month, &year ); 
          if ( res >= 0 ) { 
             switch(month){ 
                case 1:
                   printf("%d January %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 2:
                   printf("%d February %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 3:
                   printf("%d March %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 4:
                   printf("%d April %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 5:
                   printf("%d May %d is a %s \n", day, year, getday(day,month,year)); week_day(day, month, year); 
                   break; 
                case 6:
                   printf("%d June %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 7:
                   printf("%d July %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 8:
                   printf("%d August %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 9:
                   printf("%d September %d is a %s \n", day, year, getday(day,month,year)); week_day(day, month, year); 
                   break; 
                case 10:
                   printf("%d October %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 11:
                   printf("%d November %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 12:
                   printf("%d December %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                default :
                   printf("Invalid month\n"); 
                   return 0; 
             } 
          
          } 
          else{ 
             printf( "Bad day command parameters\n" ); 
          } 
          return 1; 
       } 
    
    /*******************************************/
    
               
       int do_month ( char *cmd ) { 
               
       
          int month, year; 
          int res; 
          res = sscanf( cmd, "%*c %d %d", &month, &year ); 
          if ( res == 2 ) { 
             switch(month){ 
                case 1:
                   printf("January\n"); 
                   break; 
                case 2:
                   printf("February\n"); 
                   break; 
                case 3:
                   printf("March\n"); 
                   break; 
                case 4:
                   printf("April\n"); 
                   break;
                case 5:
                   printf("May\n"); 
                   break; 
                case 6:
                   printf("June\n"); 
                   break; 
                case 7:
                   printf("July\n"); 
                   break; 
                case 8:
                   printf("August\n"); 
                   break; 
                case 9:
                   printf("September\n"); 
                   break; 
                case 10:
                   printf("October\n"); 
                   break; 
                case 11:
                   printf("November "); 
                   break; 
                case 12:
                   printf("December\n"); 
                   break; 
                default :
                   printf("Invalid month\n"); 
                   return 0; 
             } 
          } 
          else { 
             printf( "Bad month command parameters\n" ); 
          } 
          return 1; 
       } 
    
    
    
               
       void do_year ( char *cmd ) { 
               
          int year; 
          int res; 
          res = sscanf( cmd, "%*c %d", &year ); 
          if ( res == 1 ) { 
             printf("%d \n", year); 
          } 
          else { 
             printf( "Bad year command parameters\n" ); 
          } 
       } 
    
    
    
               
       int first_day_of_month ( int year, int month ) { 
               
       
          struct tm t = { 0 }; 
          struct tm *tp; 
          time_t now; 
          t.tm_mday = 1; 
          t.tm_mon = month-1; 
          t.tm_year = year-1900; 
          now = mktime(&t); 
          tp = localtime(&now); 
          return tp->tm_wday; 
       } 
    
    
    
               
       int main ( ) { 
               
       
          char buff[BUFSIZ]; 
       
          while ( fgets( buff, BUFSIZ, stdin ) != NULL ) { 
             switch ( buff[0] ) { 
                case 'd':
                   do_day ( buff ); 
                   return 0; 
                   break; 
                case 'm':
                   do_month ( buff ); 
                   return 0; 
                   break; 
                case 'y':
                   do_year ( buff ); 
                   return 0; 
                   break; 
                default:
                   printf( "Unknown command: %c\n", buff[0] ); 
                   return 0; 
                   break; 
             } 
          } 
          return 0; 
       }

  12. #12
    Registered User
    Join Date
    Sep 2002
    Posts
    31

    Question makefile



    Hi i need help still with my above Q, but i also need help with makefile. I need a makefile so that i can test the prog properly and stuff, but i dont really know if im doing it properly becuase im getting errors. Ill include my makefile first and then my program. If anyone could fix the makefile for me, i would greatly appreciate it. Thanx

    MakeFile:

    Code:
    CC=gcc
    OBJS=calen.o date.o
    FLAGS=
    
    calen: $(OBJS)
    	$(CC) -o calen $(OBJS)
    
    date.o: date.c date.h
    	$(CC) $(FLAGS) -c date.c
    
    calen.o: calen.c date.h
    	$(CC) $(FLAGS) -c calen.c
    
    clean:
    	rm -f $(OBJS) calen
    
    debug:
    	make FLAGS=-g
    
    tar:
    	tar cf try.tar Makefile calen.c date.c date.h

    My Program:

    Code:
    #include <stdio.h> 
    #include <time.h> 
    #include <sys/time.h> 
    
    /* true for leap years */ 
    
    /***************************DATE.C**************************/ 
    
    
               
       int leap_year (int year) { 
               
       
          return ((year % 400 == 0) || ((year % 100 != 0) && (year % 4 == 0))); 
       } 
    /* the number of days in month of year */ 
    
    
    
    
               
       int days_in_month (int month, int year) { 
               
       
          switch (month) { 
             case 9:case 4:case 6:case 11:
                return 30; 
                break; 
             case 2:
                return leap_year(year) ? 29: 28; 
                break; 
             default:
                return 31; 
                break; 
          } 
       } 
    
    /* the weekday (0 = Sunday) of a given date 
    accurate for dates in the Gregorian calendar which began on 14 September 1752 */ 
               
       int week_day (int day, int month, int year) { 
               
       
          int d = 14; /* The modern calendar began on 14 ... */ 
          int m = 9; /* ... September ... */ int y = 1752; /* ... 1752 ... */ 
          int n = 4; /* ... which was a Thursday */ 
          while (year > y || month > m || day > d) { 
             n++; 
             day--; 
             if (day < 1) { month--; day = days_in_month(month, year); } 
             if (month < 1) { year--; month = 12; } 
          } 
          return n % 7; 
       } 
    
       struct timeval tv; 
    
    
               
       int current_day () { 
               
       
          gettimeofday(&tv, 0); 
          return localtime(&tv.tv_sec)->tm_mday; 
       } 
    
    
    
               
       int current_month () { 
               
       
          gettimeofday(&tv, 0); 
          return localtime(&tv.tv_sec)->tm_mon + 1; 
       } 
    
    
    
    
               
       int current_year () { 
               
       
          gettimeofday(&tv, 0); 
          return localtime(&tv.tv_sec)->tm_year + 1900; 
       } 
    
    /************************MY PROGRAM**********************/ 
       static char myday[7][15]= 
       {{"Sunday"}, 
        {"Monday"}, 
        {"Tuesday"}, 
        {"Wednesday"}, 
        {"Thursday"}, 
        {"Friday"}, 
        {"Saturday"} 
       }; 
       int first_day_of_month ( int year, int month ); 
    
               
       char* getday(int d, int m, int y){ 
               
          int current_day; 
          int day = first_day_of_month( 2002, m ); 
          current_day = (day + d - 1) % 7; 
          return myday[current_day]; 
       } 
    
    
               
       int do_day ( char *cmd ) { 
               
          int day ;    
          int month ;
          int year; 
          int res; 
          res = sscanf( cmd, "%*c %d %d %d", &day, &month, &year ); 
          if ( res >= 0 ) {
             switch(month){ 
                case 1:
                   printf("%d January %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 2:
                   printf("%d February %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 3:
                   printf("%d March %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 4:
                   printf("%d April %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 5:
                   printf("%d May %d is a %s \n", day, year, getday(day,month,year)); week_day(day, month, year); 
                   break; 
                case 6:
                   printf("%d June %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 7:
                   printf("%d July %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 8:
                   printf("%d August %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 9:
                   printf("%d September %d is a %s \n", day, year, getday(day,month,year)); week_day(day, month, year); 
                   break; 
                case 10:
                   printf("%d October %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 11:
                   printf("%d November %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                case 12:
                   printf("%d December %d is a %s \n", day, year, getday(day,month,year)); 
                   week_day(day, month, year); 
                   break; 
                default :
                   printf("Invalid month\n"); 
                   return 0; 
             } 
          
          } 
          else{ 
             printf( "Bad day command parameters\n" ); 
          } 
          return 1; 
       } 
    
    
    
               
       int do_month ( char *cmd ) { 
               
       
          int month, year; 
          int res; 
          res = sscanf( cmd, "%*c %d %d", &month, &year ); 
          if ( res == 2 ) { 
             switch(month){ 
                case 1:
                   printf("January\n"); 
                   break; 
                case 2:
                   printf("February\n"); 
                   break; 
                case 3:
                   printf("March\n"); 
                   break; 
                case 4:
                   printf("April\n"); 
                   break;
                case 5:
                   printf("May\n"); 
                   break; 
                case 6:
                   printf("June\n"); 
                   break; 
                case 7:
                   printf("July\n"); 
                   break; 
                case 8:
                   printf("August\n"); 
                   break; 
                case 9:
                   printf("September\n"); 
                   break; 
                case 10:
                   printf("October\n"); 
                   break; 
                case 11:
                   printf("November "); 
                   break; 
                case 12:
                   printf("December\n"); 
                   break; 
                default :
                   printf("Invalid month\n"); 
                   return 0; 
             } 
          } 
          else { 
             printf( "Bad month command parameters\n" ); 
          } 
          return 1; 
       } 
    
    
    
               
       void do_year ( char *cmd ) { 
               
          int year; 
          int res; 
          res = sscanf( cmd, "%*c %d", &year ); 
          if ( res == 1 ) { 
             printf("%d \n", year); 
          } 
          else { 
             printf( "Bad year command parameters\n" ); 
          } 
       } 
    
    
    
               
       int first_day_of_month ( int year, int month ) { 
               
       
          struct tm t = { 0 }; 
          struct tm *tp; 
          time_t now; 
          t.tm_mday = 1; 
          t.tm_mon = month-1; 
          t.tm_year = year-1900; 
          now = mktime(&t); 
          tp = localtime(&now); 
          return tp->tm_wday; 
       } 
    
    
    
               
       int main ( ) { 
               
       
          char buff[BUFSIZ]; 
       
          while ( fgets( buff, BUFSIZ, stdin ) != NULL ) { 
             switch ( buff[0] ) { 
                case 'd':
                   do_day ( buff ); 
                   return 0; 
                   break; 
                case 'm':
                   do_month ( buff ); 
                   return 0; 
                   break; 
                case 'y':
                   do_year ( buff ); 
                   return 0; 
                   break; 
                default:
                   printf( "Unknown command: %c\n", buff[0] ); 
                   return 0; 
                   break; 
             } 
          } 
          return 0; 
       }

  13. #13
    Registered User
    Join Date
    Sep 2002
    Posts
    31
    Any ideas with the makefile thing tho?

  14. #14
    Registered User
    Join Date
    Sep 2002
    Posts
    31
    Could the problem be that the date.c file i have actually included as code in the prog makes the makefile not work? can anyone please correct my makefile so that i can test my code properly. Thanx. (In above threads.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-15-2007, 12:36 AM
  2. Very short code tt never work...please help
    By newbie1234 in forum C Programming
    Replies: 7
    Last Post: 05-23-2006, 11:46 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. this code COMPILES, but DOESNT WORK
    By Leeman_s in forum C++ Programming
    Replies: 14
    Last Post: 11-01-2002, 05:54 PM
  5. << !! Posting Code? Read this First !! >>
    By kermi3 in forum Linux Programming
    Replies: 0
    Last Post: 10-14-2002, 01:30 PM

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