Thread: Problem with structured lists from text

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    2

    Problem with structured lists from text

    New to this C stuff, and was going through Kochan's book Programming in C and got to chapter 9 structured lists.

    There's this problem that requires you to set a Variable N that calculates days.

    N = 1461 x f(year, month) / 4 + 153 x g(month) / 5 + day

    where
    f(year, month) = year-1 (if month <=2)
    =year (otherwise)

    g(month) = month+13 (if month <=2)
    = month+1 (otherwise)
    Code:
    struct date { 
    
    
    int month;
    int day; 
    int year; 
    
    
    };
    
    
    struct date daycalc (struct date entry){
    struct date count;
    
    
    
    
    if(entry.month <=2){
    entry.year-1;
    entry.month + 13;
    }
    
    
    else{
    entry.year ;
    entry.month+1;
    }
    
    
    return entry;
    
    
    }
    
    
    void main (){
    
    
    struct date daycalc (struct date entry); 
    struct date firstDay, secondDay, T1;
    int N1;
    
    
    printf("Please enter mm/dd/yyyy: ");
    scanf("%i%i%i", &firstDay.month, &firstDay.day, &firstDay.year);
    T1=daycalc(firstDay);
    
    
    
    
    ;N1=( 1461 * T1.year ) / 4 + ( 153 * T1.month ) / 5 + T1.day;
    
    }
    I was having issues getting the right values for N so I tried to see what
    T1.year gives me when I type in firstDay.month=02, firstDay.day=08 and firstDay.year=1999, and I got 8. According to book I should be getting 1998.

    What did I do wrong?

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    1,066
    For a start you could look at the compiler warnings:
    Code:
    e$ make foo
    cc -ggdb3 -Wall -Wextra    foo.c   -o foo
    foo.c: In function ‘daycalc’:
    foo.c:19:1: warning: statement with no effect [-Wunused-value]
    foo.c:20:1: warning: statement with no effect [-Wunused-value]
    foo.c:25:1: warning: statement with no effect [-Wunused-value]
    foo.c:26:1: warning: statement with no effect [-Wunused-value]
    foo.c:13:13: warning: unused variable ‘count’ [-Wunused-variable]
    foo.c: At top level:
    foo.c:36:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
    foo.c: In function ‘main’:
    foo.c:44:1: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
    foo.c:44:1: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
    foo.c:45:1: warning: implicit declaration of function ‘scanf’ [-Wimplicit-function-declaration]
    foo.c:45:1: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
    foo.c:41:5: warning: variable ‘N1’ set but not used [-Wunused-but-set-variable]
    foo.c:40:23: warning: unused variable ‘secondDay’ [-Wunused-variable]
    Bye, Andreas
    Last edited by AndiPersti; 04-05-2013 at 03:13 PM.

  3. #3
    Registered User
    Join Date
    Mar 2013
    Posts
    2
    Quote Originally Posted by AndiPersti View Post
    For a start you could look at the compiler warnings:
    Code:
    e$ make foo
    cc -ggdb3 -Wall -Wextra    foo.c   -o foo
    foo.c: In function ‘daycalc’:
    foo.c:19:1: warning: statement with no effect [-Wunused-value]
    foo.c:20:1: warning: statement with no effect [-Wunused-value]
    foo.c:25:1: warning: statement with no effect [-Wunused-value]
    foo.c:26:1: warning: statement with no effect [-Wunused-value]
    foo.c:13:13: warning: unused variable ‘count’ [-Wunused-variable]
    foo.c: At top level:
    foo.c:36:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
    foo.c: In function ‘main’:
    foo.c:44:1: warning: implicit declaration of function ‘printf’ [-Wimplicit-function-declaration]
    foo.c:44:1: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
    foo.c:45:1: warning: implicit declaration of function ‘scanf’ [-Wimplicit-function-declaration]
    foo.c:45:1: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
    foo.c:41:5: warning: variable ‘N1’ set but not used [-Wunused-but-set-variable]
    foo.c:40:23: warning: unused variable ‘secondDay’ [-Wunused-variable]
    Bye, Andreas
    I accidentally posted it with a typo. Here's the one that should work. Thanks.
    Code:
    #include <stdio.h>
    
    
    struct date {
    
    
    int month;
    int day;
    int year; 
    
    
    };
    
    
    struct date daycalc (struct date entry){
    
    
    
    
    
    
    if(entry.month <=2){
    entry.year-1;
    entry.month + 13;
    }
    
    
    else{
    entry.year ;
    entry.month+1;
    }
    
    
    return entry;
    
    
    }
    
    
    void main (){
    
    
    struct date daycalc (struct date entry);
    struct date firstDay, secondDay, T1;
    int N1;
    
    
    printf("Please enter mm/dd/yyyy: ");
    scanf("%i%i%i", &firstDay.month, &firstDay.day, &firstDay.year);
    T1=daycalc(firstDay);
    
    
    
    
    N1=( 1461 * T1.year ) / 4 + ( 153 * T1.month ) / 5 + T1.day;
    
    
    //printf("%i", N1);
    printf("%i", T1.year);
    
    
    
    
    }
    Nevermind I see what you mean. My compiler doesn't have any of those warnings.

    Ok, I see how you got those warnings. Aw this makes sense. Anyway thanks once again.
    Last edited by Sertaba; 04-05-2013 at 03:26 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. linked lists, text files, and classes
    By mbarbay in forum C++ Programming
    Replies: 10
    Last Post: 02-09-2010, 10:49 AM
  2. sorting structured array problem
    By matrix0978 in forum C Programming
    Replies: 7
    Last Post: 11-12-2009, 01:42 AM
  3. Structured exception
    By George2 in forum Windows Programming
    Replies: 4
    Last Post: 01-20-2008, 08:40 AM
  4. Replies: 5
    Last Post: 08-01-2007, 06:17 AM