debug program

This is a discussion on debug program within the C Programming forums, part of the General Programming Boards category; Why my program always print out sunday? What's wrong with it and how to fix it? help please.......THANK #include<stdio.h> /****** ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    1

    Unhappy debug program

    Why my program always print out sunday?

    What's wrong with it and how to fix it?

    help please.......THANK

    #include<stdio.h>

    /****** Global Declarations ******/
    int month;
    int day;
    int year;
    int db;
    int dotw;
    int dob;
    int daydiv;

    /****** Prototype Declarations ******/

    void getDATA (int month, int day, int year);
    void calcDOTW (int year, int dotw);
    void calcLEAPYEAR (int year, int daydiv);
    void calcMONTH (int day, int daydiv, int dob, int dotw, int month, int db);
    void calcDOB (int dob, int db);

    /****** MAIN ******/

    int main()
    {
    getDATA (month, day, year);
    calcDOTW (year, dotw);
    calcLEAPYEAR (year, daydiv);
    calcMONTH (day, daydiv, dob, dotw, month, db);
    calcDOB (dob, db);

    return 0;
    }

    /****** getDATA ******/

    void getDATA (int month, int day, int year)
    {
    printf("Enter the birth date in MM-DD-YYYY format: ");
    scanf("%d-%d-%d", &month, &day, &year);
    }

    /****** Calculate day of Dec. 31st of the last year ******/

    void calcDOTW (int year, int dotw)
    {
    dotw = (((year - 1) * 365) + ((year - 1) / 4) - ((year -1) / 100) + ((year -1) / 400)) % 7;

    return;
    }

    /****** Determine if the year is a LEAP YEAR ******/

    void calcLEAPYEAR (int year, int daydiv)
    {
    daydiv = 0;

    if((!(year % 4) && (year % 100)) || !(year % 400))

    if(month > 2 || month == 2 && day > 28)
    daydiv += 1;
    else
    daydiv = daydiv;
    else
    daydiv = daydiv;
    }

    /****** Sum up the days in all the months ******/

    void calcMONTH (int day, int daydiv, int dob, int dotw, int month, int db)
    {
    dob = 0;

    switch (month)
    {
    case 1: daydiv = (day) / 7;
    dob = (day) - (daydiv * 7) + dotw;
    break;

    case 2: daydiv = (day + 31) / 7;
    dob = (day + 31) - (daydiv * 7) + dotw;
    break;

    case 3: daydiv = (day + 31 + 28) / 7;
    dob = (day + 31 + 28) - (daydiv * 7) + dotw;
    break;

    case 4: daydiv = (day + 31 + 28 + 31) / 7;
    dob = (day + 31 + 28 + 31) - (daydiv * 7) + dotw;
    break;

    case 5: daydiv = (day + 31 + 28 + 31 + 30) / 7;
    dob = (day + 31 + 28 + 31 + 30) - (daydiv * 7) + dotw;
    break;

    case 6: daydiv = (day + 31 + 28 + 31 + 30 + 31) / 7;
    dob = (day + 31 + 28 + 31 + 30 + 31) - (daydiv *7) + dotw;
    break;

    case 7: daydiv = (day + 31 + 28 + 31 + 30 + 31 + 30) / 7;
    dob = (day + 31 + 28 + 31 + 30 + 31 + 30) -(daydiv * 7) + dotw;
    break;

    case 8: daydiv = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31) /7;
    dob = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31) -(daydiv * 7) + dotw;
    break;

    case 9: daydiv = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31 +31) / 7;
    dob = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31 +31) - (daydiv * 7) + dotw;
    break;

    case 10: daydiv = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31 +31 + 30) / 7;
    dob = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31 +31 + 30) - (daydiv * 7) + dotw;
    break;

    case 11: daydiv = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31 +31 + 30 + 31) / 7;
    dob = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31 +31 + 30 + 31) - (daydiv * 7) + dotw;
    break;

    case 12: daydiv = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31 +31 + 30 + 31 + 30) / 7;
    dob = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31 +31 + 30 + 31 + 30) - (daydiv * 7) + dotw;
    break;
    }
    }

    /****** Calculate Day of Birth ******/

    void calcDOB (int db, int dob)
    {
    db = (dob) - ((dob / 7) * 7);

    switch (db)
    {
    case 0: printf("\nYou were born on a Sunday\n");
    break;

    case 1: printf("\nYou were born on a Monday\n");
    break;

    case 2: printf("\nYou were born on a Tuesday\n");
    break;

    case 3: printf("\nYou were born on a Wednesday\n");
    break;

    case 4: printf("\nYou were born on a Thursday\n");
    break;

    case 5: printf("\nYou were born on a Friday\n");
    break;

    case 6: printf("\nYou were born on a Saturday\n");
    break;
    }
    }

  2. #2
    Unregistered
    Guest
    The problem is that in your main declaration, you have passed the "values" of the global variables so when it gets the data it never saves it, if you remove all the variables from the function calls then it will work or at least it did for me (gave me different dates, not sure if its the right ones), the only other solution is to pass the memory address of the variables you want to change in specific functions but since there already global then this is the easiest way.

    #include<stdio.h>

    /****** Global Declarations ******/
    int month;
    int day;
    int year;
    int db;
    int dotw;
    int dob;
    int daydiv;

    /****** Prototype Declarations ******/

    void getDATA();
    void calcDOTW();
    void calcLEAPYEAR();
    void calcMONTH();
    void calcDOB();

    /****** MAIN ******/

    int main()
    {
    getDATA();
    calcDOTW();
    calcLEAPYEAR();
    calcMONTH();
    calcDOB();

    return 1;
    }

    /****** getDATA ******/

    void getDATA()
    {
    printf("Enter the birth date in MM-DD-YYYY format: ");
    scanf("%d-%d-%d", &month, &day, &year);
    }

    /****** Calculate day of Dec. 31st of the last year ******/

    void calcDOTW()
    {
    dotw = (((year - 1) * 365) + ((year - 1) / 4) - ((year -1) / 100) + ((year -1) / 400)) % 7;
    }

    /****** Determine if the year is a LEAP YEAR ******/

    void calcLEAPYEAR()
    {
    daydiv = 0;

    if((!(year % 4) && (year % 100)) || !(year % 400))

    if(month > 2 || month == 2 && day > 28)
    daydiv += 1;
    else
    daydiv = daydiv;
    else
    daydiv = daydiv;
    }

    /****** Sum up the days in all the months ******/

    void calcMONTH()
    {
    dob = 0;

    switch (month)
    {
    case 1: daydiv = (day) / 7;
    dob = (day) - (daydiv * 7) + dotw;
    break;

    case 2: daydiv = (day + 31) / 7;
    dob = (day + 31) - (daydiv * 7) + dotw;
    break;

    case 3: daydiv = (day + 31 + 28) / 7;
    dob = (day + 31 + 28) - (daydiv * 7) + dotw;
    break;

    case 4: daydiv = (day + 31 + 28 + 31) / 7;
    dob = (day + 31 + 28 + 31) - (daydiv * 7) + dotw;
    break;

    case 5: daydiv = (day + 31 + 28 + 31 + 30) / 7;
    dob = (day + 31 + 28 + 31 + 30) - (daydiv * 7) + dotw;
    break;

    case 6: daydiv = (day + 31 + 28 + 31 + 30 + 31) / 7;
    dob = (day + 31 + 28 + 31 + 30 + 31) - (daydiv *7) + dotw;
    break;

    case 7: daydiv = (day + 31 + 28 + 31 + 30 + 31 + 30) / 7;
    dob = (day + 31 + 28 + 31 + 30 + 31 + 30) -(daydiv * 7) + dotw;
    break;

    case 8: daydiv = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31) /7;
    dob = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31) -(daydiv * 7) + dotw;
    break;

    case 9: daydiv = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31 +31) / 7;
    dob = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31 +31) - (daydiv * 7) + dotw;
    break;

    case 10: daydiv = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31 +31 + 30) / 7;
    dob = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31 +31 + 30) - (daydiv * 7) + dotw;
    break;

    case 11: daydiv = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31 +31 + 30 + 31) / 7;
    dob = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31 +31 + 30 + 31) - (daydiv * 7) + dotw;
    break;

    case 12: daydiv = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31 +31 + 30 + 31 + 30) / 7;
    dob = (day + 31 + 28 + 31 + 30 + 31 + 30 + 31 +31 + 30 + 31 + 30) - (daydiv * 7) + dotw;
    break;
    }
    }

    /****** Calculate Day of Birth ******/

    void calcDOB()
    {
    db = (dob) - ((dob / 7) * 7);

    switch (db)
    {
    case 0: printf("\nYou were born on a Sunday\n");
    break;

    case 1: printf("\nYou were born on a Monday\n");
    break;

    case 2: printf("\nYou were born on a Tuesday\n");
    break;

    case 3: printf("\nYou were born on a Wednesday\n");
    break;

    case 4: printf("\nYou were born on a Thursday\n");
    break;

    case 5: printf("\nYou were born on a Friday\n");
    break;

    case 6: printf("\nYou were born on a Saturday\n");
    break;
    }
    }

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > The problem is that in your main declaration, you have passed
    > the "values" of the global variables so when it gets the data it
    > never saves it, if you remove all the variables from the function
    > calls then it will work or at least it did for me (gave me different
    > dates, not sure if its the right ones), the only other solution is
    > to pass the memory address of the variables you want to
    > change in specific functions but since there already global then
    > this is the easiest way.

    This is wrong. It's "global", and as such, all functions in this file automaticly can see and change the values of all global variables. There is no need to pass them as anything.

    I suspect the real problem is that in 'calcMONTH', 'dob' is never used. It is given a value that never actually is assigned to anything.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Unregistered
    Guest
    Possibly, but I compiled the code and it kept giving sunday as mentioned, and since all variables are global then in a function they should be able to access and modify them, this is true, however if the function get the value passed with the same name as a global variable then when those variables get accessed inside the function it referes to the value being passed and not the global variable, umm dob is a global variable, the value is set to 0 and then changed, I'm not saying that the code gives the exact right day but it does give different days when you put in different days.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with my program...
    By Noah in forum C Programming
    Replies: 2
    Last Post: 03-11-2006, 07:49 PM
  2. Plz help me to debug my program
    By Bage in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2004, 01:54 PM
  3. my server program auto shut down
    By hanhao in forum Networking/Device Communication
    Replies: 1
    Last Post: 03-13-2004, 10:49 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 11:04 PM

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