Thread: wrong answer?

  1. #1
    cman
    Guest

    wrong answer?

    the program is meant to output 31 days
    why does it output 30 days?
    what have i done wrong

    Code:
    int main()
    {
    	int month, days;
    
    	month = 7;
    	
    	if (month == 2) { 
    		days = 28;
    	}
    	else if (month == 4 || 6 || 9 || 11 ) {
    		days = 30;
    	}
    	else {
    		days = 31;
    
    	}	
    	
    	printf( "days=%d\n", days );
    
    	return 0;
    }

  2. #2
    .........
    Join Date
    Nov 2002
    Posts
    303
    This
    Code:
    else if (month == 4 || 6 || 9 || 11 ) {
    Should be
    Code:
    else if (month == 4 || month == 6 || month == 9 || month == 11 ) {
    You would probably be better off with a switch statement, more
    readable.

  3. #3
    cman
    Guest
    hmm
    why wont ( month == 4 || 6 || 9 || 11 ) work?
    is this statement incomplete if its written like this?

  4. #4
    .........
    Join Date
    Nov 2002
    Posts
    303
    Yea that isn't valid.

    EDIT - If you want to know why its because of the way it is evaluated, its called Short Circuit Evaluation, you can google for it if your really interested. But basically it stops evaluating operands of logical operation once the result is known.
    Last edited by SourceCode; 03-08-2003 at 09:05 PM.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    78
    No, the reason it outputs 30 is because if(6) is always true. The first expression, month==4, evaluates to false. After the first expression is evaluated to false the OR is evaluated and the next expression must be evaluated. Since 6 is a non-zero value the expression evaluates to true. Because only one of the expressions must evaluate true, the if evaluates true and the code block is executed. The if statement is valid, it just doesn't contain the correct logic for what you are trying to do. Remember: in C if(<zero>) == true, if(<any non-zero value>) == false.
    Last edited by Rog; 03-08-2003 at 11:06 PM.

  6. #6
    Registered User rmullen3's Avatar
    Join Date
    Nov 2001
    Posts
    330

    ~

    Here's the code using a switch statement, which is prettier:

    Code:
    switch (month)
    {
         case 2: days = 28; break;
         case 4:
         case 6:
         case 9:
         case 11: days = 30; break;
         default: days = 31; break;
    }
    "He who makes a beast of himself, gets rid of the pain of being a man." Dr. Johnson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question 26 Answer seems wrong on site quiz
    By Kleid-0 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2005, 03:03 PM
  2. whats wrong with this? no errors but wrong result
    By InvariantLoop in forum C Programming
    Replies: 6
    Last Post: 01-28-2005, 12:48 AM
  3. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 12:01 PM
  5. Half life Problem which I am right and the teacher is wrong
    By Shadow12345 in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 11-06-2002, 04:28 PM