Thread: My if-else statements won't execute

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    My if-else statements won't execute

    So I'm trying to create a program that will advance a user-specified calendar date by a user-specified number of days. So I created a while loop that should run through my if-else statements to appropriately advance the date, but for some reason, none of the if-else statements execute.

    numDays = the number of days by which the date should be advanced

    day = current day of the month

    Code:
    int x; 
    	while (x = 0, x <= numDays, x++){
    
    
    		if (month == 1 && ((day + numDays) < 31)){
    
    			day++;
    
    		}
    		else if (month == 1 && (day + numDays > 31) && (isLeapYear(year) == true) && (day + numDays <= 60))
    		{
    
    			month = 2;
    			day = day - 31;
    
    		}
    		else if (month == 1 && (day + numDays > 31) && (isLeapYear(year) == false) && (day + numDays <= 59))
    		{
    
    			month = 2;
    			day = day - 31;
    
    		}
    		else if (month == 2 && (day + numDays > 29) && (isLeapYear(year) == true) && (day + numDays <= 60)){
    
    			month = 3;
    			day = day - 29;
    
    		}
    		else if (month == 2 && (day + numDays > 28) && (isLeapYear(year) == false) && (day + numDays <= 59)){
    
    			month = 3;
    			day = day - 28;
    		}
    		else if (month == 3 && (day + numDays > 31) && (day + numDays <= 61)){
    
    			month = 4;
    			day = day - 31;
    
    		}
    		else if (month == 4 && (day + numDays > 30) && (day + numDays <= 61)){
    
    			month = 5;
    			day = day - 30;
    
    		}
    		else if (month == 5 && (day + numDays > 31) && (day + numDays <= 61)){
    
    			month = 6;
    			day = day - 31;
    
    		}
    		else if (month == 6 && (day + numDays > 30) && (day + numDays <= 61)){
    
    			month = 7;
    			day = day - 30;
    
    		}
    		else if (month == 7 && (day + numDays > 31) && (day + numDays <= 62)){
    
    			month = 8;
    			day = day - 31;
    
    		}
    		else if (month == 8 && (day + numDays > 31) && (day + numDays < 61)){
    
    			month = 9;
    			day = day - 31;
    
    		}
    		else if (month == 9 && (day + numDays > 30) && (day + numDays <= 61)){
    
    			month = 10;
    			day = day - 30;
    
    		}
    		else if (month == 10 && (day + numDays > 31) && (day + numDays <= 61)){
    
    			month = 11;
    			day = day - 31;
    
    		}
    		else if (month == 11 && (day + numDays > 30) && (day + numDays <= 61)){
    
    			month = 12;
    			day = day - 30;
    
    		}
    		else if (month == 12 && (day + numDays > 31) && (day + numDays <= 62)){
    
    			month = 1;
    			day = day - 31;
    			year++;
    
    		}
    		else{
    
    			day = day + 1;
    
    		}
    
    
    
    	}
    I have compiled and run the program and when I enter a date, such as 1/4/2016, and I enter 4 for the number of days to advance, it doesn't advance the date at all. It stays at 1/4/2016. Could someone enlighten me on what I'm doing wrong that is causing my if-else statements to not work?

    Any help that you could offer is greatly appreciated!

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    You have confused a for loop with a while loop. It compiles because the comma is actually an operator, that executes everything on the left size and returns the right-most one( in your case zero, which is equivalent to false ).
    You'd want this line instead:
    Code:
     for (x = 0; x <= numDays; x++){
    Devoted my life to programming...

  3. #3
    Unregistered User Yarin's Avatar
    Join Date
    Jul 2007
    Posts
    2,158
    "Three or more, use a for"

  4. #4
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Oh good catch. Thank you for your help! I don't know why I didn't think of that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execute PHP
    By david84 in forum C++ Programming
    Replies: 3
    Last Post: 09-12-2010, 05:06 AM
  2. Execute Once
    By siavoshkc in forum Windows Programming
    Replies: 3
    Last Post: 02-28-2006, 11:02 AM
  3. Won't Execute, Help Please.
    By wazx in forum C Programming
    Replies: 4
    Last Post: 01-26-2006, 04:05 AM
  4. Replies: 3
    Last Post: 11-08-2005, 07:25 AM
  5. How do i execute a .exe during runtime?
    By Fender Bender in forum C Programming
    Replies: 5
    Last Post: 04-17-2005, 02:46 AM

Tags for this Thread