Results 1 to 3 of 3

Thread: Conditional Operator

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    281

    Resolved Conditional Operator

    I have a structure:
    Code:
    struct Time {
        int time;
        int hour;
        int second;
    };
    and I have 2 functions:
    Code:
    void printMilitary( const Time &t )
    {
    	cout << ( t.hour < 10 ? "0" : "" ) << t.hour << ":"
    		<< ( t.minute < 10 ? "0" : "" ) << t.minute;
    }
    
    void printStandard( const Time &t )
    {
    	cout << ( ( t.hour == 0 || t.hour == 12 ) ?
    		12 : t.hour % 12 )
    		<< ":" << ( t.minute < 10 ? "0" : "" ) << t.minute
    		<< ":" << ( t.second < 10 ? "0" : "" ) << t.second
    		<< ( t.hour < 12 ? " AM" : " PM" );
    }
    They are used like this:
    Code:
    int main()
    {
    	Time dinnerTime;
    
    	dinnerTime.hour = 18;
    	dinnerTime.minute = 30;
    	dinnerTime.second = 0;
    
    	cout << "Dinner will be held at: ";
    	printMilitary( dinnerTime );
    	cout << " military time, \nwhich is ";
    	printStandard( dinnerTime );
    	cout << " standard time.\n";
    What the code is doing, if you haven't already noticed, is taking military time format and displaying it as standard time format. My question is about the conditional operator ( ?: ). How is it taking the military time and converting it to standard time? If you really dont want to go into detail just comment the code I have listed.
    Code:
    	cout << ( ( t.hour == 0 || t.hour == 12 ) ?
    		12 : t.hour % 12 )
    		<< ":" << ( t.minute < 10 ? "0" : "" ) << t.minute
    		<< ":" << ( t.second < 10 ? "0" : "" ) << t.second
    		<< ( t.hour < 12 ? " AM" : " PM" );
    Last edited by KodeShark; Sep 24th, 2004 at 05:37 PM.

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2004
    Posts
    281
    I think I get it now:

    Code:
    cout << ( ( t.hour == 0 | t.hour == 12 ) ? 12 : t.hour % 12 )
    is the main part.

    If t.hour is equal to 0 or 12 then 12 is the answer. otherwise t.hour MOD 12.

    So if t.hour = 14 then

    cout << ( ( 14 == 0 or 14 == 12 ) ? 12 : 14 % 12

    obviously 14 isnt equal to 0 or 12 so the answer isnt 12. So 14%12 is the answer.

    14 % 12 = 2 because 14/12 = 1 and 14 - ( 12 * 1 ) = 2

    then you have:
    << ":" ( t.minute < 10 ? "0" : "" ) << t.minute

    which is ( 30 < 10 ? "0" : "" )
    30 isn't less than 10 so "0" isnt the answer

    so if you have the minute as 9 you will get "09"

    so far:
    t.hour = 14
    t.minute = 9
    2:09

    seconds works like minutes~

  3. #3
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403
    the ? : operator works like this (Expression ? True Part : False Part)

    it's an inline if statement.

    Code:
    cout << ( ( t.hour == 0 || t.hour == 12 ) ? 12 : t.hour % 12 )
    is the same as writing

    Code:
    if (t.hour == 0 || t.hour == 12)
       cout << 12;
    else
       cout << t.hour % 12
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width