looping problem

This is a discussion on looping problem within the C Programming forums, part of the General Programming Boards category; I have created two functions one to test if a year is a leap year and another to test for ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    12

    looping problem

    I have created two functions one to test if a year is a leap year and another to test for a valid day and month and the code works perfectly. But im having a problem if the date is wrong I want the user to keep entering data until they enter the correct date and if they enter the correct date I want the information stored in the struct. Currently the while loop keeps looping if the date is correct or wrong. The if statement (if (CAPACITY < count)) allows a user to enter one item at a time. Can someone help?

    Code:
    #include<stdio.h>
    #include<ctype.h>
    #include<stdlib.h>
    
    #define  bool int
    #define YES 1
    #define NO 0
    #define CAPACITY 20
    #define SIZE 50
    #define TRUE 1
    #define FALSE 0
    
    
    typedef struct
    {
    	int day;
    	int month;
    	int year;
    }Date;
    
    typedef struct
    {
    	char name[SIZE];
    	Date dob;
    }Student;
    
    void checkDate(Student data[], int count);
    bool isLeapYear(int year);
    
    int main (void)
    {
    	bool  check = FALSE;
    	int i, count = 0;
    	Student database[CAPACITY];
    
    
    			 if (CAPACITY > count)
    				{
    					while(check == FALSE)
    					{
    						printf("Please enter a person's first name, lastname and date of birth\n");
    					scanf("%s %s %d %d %d", database[count].name, &database[count].dob.day, &database[count].dob.month, &database[count].dob.year);
    				
    					
    					checkDate(database, count);
    
    					}/*end while(check == FALSE) */
    					
    					count++;
    				 
    				}/*end if (CAPACITY > count) */
    						else
    						{
    							printf("The database is full\n");
    						}
    				
    	return 0;
    }/*end main() function */
    
    
    /***************************************************************************/
    /*Function to check valid date                                             */
    /***************************************************************************/
    void checkDate(Student data[], int count)
    {
    	int monthLength[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    	bool check = FALSE;	
    
    	/*Add 29 days to Feburary if year is a leap year*/
    	if (isLeapYear(data[count].dob.year))
    	{
    		monthLength[2] = 29;
    	}
    
    	/*If month entered is less then one and greater than 12 print error message*/
    	if (data[count].dob.month < 1 || data[count].dob.month > 12)
    	{
    		printf("Invalid month\n");
    		check = FALSE;
    	}
    	/*If day entered is less then 1 and greater then month number in the array month_length print error message*/
    	else if (data[count].dob.day < 1 || data[count].dob.day > monthLength[data[count].dob.month])
    	{
    		printf("Invalid day\n");
    		check = FALSE;
    	}
    
    }/* end bool checkDate(Student data[], int count) */
    
    /***************************************************************************/
    /*Function to check if a year is a leap year                               */
    /***************************************************************************/
    bool isLeapYear(int year)
    {
    	bool valid;
    
    	if ((year%4) !=0)
    	{
    		valid = NO;
    	}
    	else if ((year%400) == 0)
    	{
    		valid = YES;
    	}
    	else if ((year%100) == 0)
    	{
    		valid = NO;
    	}
    	else
    	{
    		valid = YES;
    	}
    
    	return (valid);
    }/*end bool isLeapYear(int year) */

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    I have a few comments. don't you have like std_bool for a header or something like that?
    What is the significant difference between the TRUE and FALSE and YES and NO macros?

    Don't use scanf to do all the work. Read in a string with fgets and parse that instead.

    As far as I know, leap years divide by four evenly. Your isLeapYear function does way too much work, and it also returns a void, when you say it should return a bool. No wonder you loop forever, the variable check is never updated.
    Last edited by whiteflags; 05-20-2006 at 05:19 PM.

  3. #3
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >>Your isLeapYear function does way too much work

    No its not, read this

    [edit]
    that leap year code was wrong - sorry guys.
    [/edit]

    Your primary problem with that loop is that check variable is
    never updated as citizen said - your checkDate function is going
    to have to return a value. Also, when you fix that issue, you'll
    notice that the program will terminate after entering only 1 value -
    change

    Code:
    if (CAPACITY > count)
    
    /*to*/
    
    while (CAPACITY > count)

    and remove the corresponding else statement:

    Code:
    else
    {
            printf("The database is full\n");
    }
    
    /*to just*/
    
            printf("The database is full\n");
    Last edited by Richie T; 05-21-2006 at 12:59 AM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Interestingly enough, there is an official comp.lang.c FAQ entry which also might shed some additional light on the matter of leap years with regard to coding such a test in C.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use a lookup table.


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

  6. #6
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    Quote Originally Posted by quzah
    Use a lookup table.


    Quzah.
    For determining leap years, you mean?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Definately.


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

  8. #8
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    You know, I was just reading a post on this subject where one of our members implemented the leap year check with a function macro - yet another way of doing it. This sort of question comes up enough every year, it would almost be cool to have a few of these different ideas examined in a separate board FAQ with a smidge of code to demonstrate the various methods of achieving the same end, with some discussion on the pros and cons of each.

  9. #9
    Registered User
    Join Date
    Mar 2006
    Posts
    12
    I have updated the code to bool like you said and return check but the loop still does not break.

    Code:
    bool checkDate(Student data[], int count)
    {
    	int monthLength[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    	bool check = FALSE;	
    
    	/*Add 29 days to Feburary if year is a leap year*/
    	if (isLeapYear(data[count].dob.year))
    	{
    		monthLength[2] = 29;
    	}
    
    	/*If month entered is less then one and greater than 12 print error message*/
    	if (data[count].dob.month < 1 || data[count].dob.month > 12)
    	{
    		printf("Invalid month\n");
    		check = FALSE;
    	}
    	/*If day entered is less then 1 and greater then month number in the array month_length print error message*/
    	else if (data[count].dob.day < 1 || data[count].dob.day > monthLength[data[count].dob.month])
    	{
    		printf("Invalid day\n");
    		check = FALSE;
    	}
             else
            {
                  check = TRUE;
            }
    return check;
    
    }/* end bool checkDate(Student data[], int count) */

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,703
    In main:
    Code:
    check = checkDate(database, count);

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    12
    Thanks very much citizen you solved my problem and thank you everyone else for your help and advice also

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Words and lines count problem
    By emo in forum C Programming
    Replies: 1
    Last Post: 07-12-2005, 03:36 PM
  2. looping problem
    By chris285 in forum C++ Programming
    Replies: 4
    Last Post: 04-22-2005, 11:03 AM
  3. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM
  4. binary tree problem - help needed
    By sanju in forum C Programming
    Replies: 4
    Last Post: 10-16-2002, 05:18 AM
  5. Looping problem
    By sketchit in forum C Programming
    Replies: 2
    Last Post: 10-01-2001, 02:19 PM

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