Thread: 2 assignmentss from one if.

  1. #1
    Registered User
    Join Date
    Oct 2011
    Location
    Raymond Terrace, New South Wales, Australia
    Posts
    7

    2 assignmentss from one if.

    I am doing a program exercise form c a modern approach chapter 5. I resolved the program to work but then have been playing with it, introduced error checking etc.

    My question is can one if result in two assignments?
    This is my code.
    Code:
    #include <stdio.h>
    
    int main(void){
        
        int hour, min, myhour;
        char * postfix;
    
    
        printf("Print the time in 24hour format: ");
        scanf("%d:%d", &hour, &min);
        
        if ((hour < 25 && hour > 0) && (min < 61 && min > 0 )){
                if (hour > 12)
                    myhour = (hour - 12) && postfix = "PM";
                else
                    myhour = hour && postfix = "AM";
    
    
                printf("The time is %.2d:%.2d %s \n", myhour, min, postfix);
                }
                
                else 
                    printf("Your input was invalid \n");
    
    
                
    
    
                
        return 0;
    }
    Basically the purpose is to convert 24 hour time to 12 hour time. I introduced
    Code:
    if ((hour < 25 && hour > 0) && (min < 61 && min > 0 ))
    to make sure my input was valid.

    But see my if subsequent to that I want to say if hours is greater than 12 minus 12 from the hours given and assign to myhour variable and assign "PM" to my postfix variable. It's not working I am getting an lvalue error.

  2. #2
    Registered User
    Join Date
    Dec 2011
    Location
    saskatoon
    Posts
    3
    The error is in your assignment
    myhour = (hour - 12) && postfix = "PM";
    && is used for a conjunction of test conditions, and is invalid in the way you used it. You will need two seperate statements for your assignments.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    && is a logical or boolean operator, not an instruction to do more.

    If you want two assignments, you need two statements.

    if() and else() both are followed by one statement. A compound statement, or block, is 1 statement. 3 examples to help you understand:
    Code:
    if( a < b )
       a = b ;   
    else
       b = a ;
    
    if( a < b )
    {                       //start of compound statement
       a = b ;
       b = c ;
    }                      //end compound
    else
    {
       b = a ;
       a = c ;
    }
    
    if( a < b ) ;  //<--- Null statement, perfectly legal, most likely an error though
      a = b ;      //<--- Always executes. Not tied to the if.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Tclausex View Post
    If you want two assignments, you need two statements.
    Not true.

    Firstly, a block (which is what your examples show) is formally a type of statement.

    Even ignoring that, an assignment is an expression, and there can be multiple expressions in a single statement. The trick is using operators with different precedence. For example, grouping in braces, the comma operator, etc.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by grumpy View Post
    Firstly, a block (which is what your examples show) is formally a type of statement.
    I believe that's what I said.
    Even ignoring that, an assignment is an expression, and there can be multiple expressions in a single statement. The trick is using operators with different precedence. For example, grouping in braces, the comma operator, etc.
    That is completely valid, but I refrain from indulging someone in abusing the comma operator. There can be nothing clearer than two separate statements for two separate assignments.

  6. #6
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If you want two assignments, you need two statements.
    a = b = c;

  7. #7
    Registered User
    Join Date
    Oct 2011
    Location
    Raymond Terrace, New South Wales, Australia
    Posts
    7
    I have updated the program however it is warning that my second else needs a "(" or identifier first.

    Code:
    #include <stdio.h>
    
    int main(void){
    	
    	int hour, min, myhour;
    	char * postfix;
    
    
    	printf("Print the time in 24hour format: ");
    	scanf("%d:%d", &hour, &min);
    	
    	if ((hour < 25 && hour > 0) && (min < 61 && min > 0 )){
    			if (hour > 12)
    				myhour = (hour - 12);
    		       		postfix = "PM";
    			else
    				myhour = hour;
    		       		postfix = "AM";
    
    
    			printf("The time is %.2d:%.2d %s \n", myhour, min, postfix);
    			}
    			
    	else 
    		printf("Your input was invalid \n");
    			
    	return 0;
    }

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Fully brace all your code until you understand what not bracing means.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Quote Originally Posted by whiteflags View Post
    a = b = c;
    Man, we're getting picky aren't we? In the context of what the OP is trying to do...

    myhour = postfix = "PM"; ????

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sayth
    I have updated the program however it is warning that my second else needs a "(" or identifier first.
    As was mentioned to you: start using braces to group statements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Tclausex View Post
    Man, we're getting picky aren't we?

    myhour = postfix = "PM"; ????
    Well that or you're getting sloppy. Your example demonstrates that a and b were assignable, as was c, to either thing. That's not what the OP was doing either. If I made a mistake, so did you.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Sayth View Post
    I have updated the program however it is warning that my second else needs a "(" or identifier first.
    Like this...
    Code:
    #include <stdio.h>
    
    int main(void)
      {
         int hour, min, myhour;
         char * postfix;
    
         printf("Print the time in 24hour format: ");
         scanf("%d:%d", &hour, &min);
    	
         if ((hour < 25 && hour > 0) && (min < 61 && min > 0 ))
           {
              if (hour > 12)
                {
                   myhour = (hour - 12);
                   postfix = "PM";
                }  
             else
               {
                  myhour = hour;
                  postfix = "AM";
               }
            printf("The time is %.2d:%.2d %s \n", myhour, min, postfix);
          }
       else 
         printf("Your input was invalid \n");
    			
         return 0;
    }
    A much simpler way would be like this...

    Code:
    #include <stdio.h>
    
    int main(void)
      {
         int hour, min, myhour;
         char * postfix;
    
         printf("Print the time in 24hour format: ");
         scanf("%d:%d", &hour, &min);
    	
         if ((hour > 24 || hour < 0) || (min < 1 || min > 59 ))
           { 
              printf("Your input was invalid \n");
              return 0;
           }
    
         if (hour > 12)
                {
                   hour -= 12);
                   postfix = "PM";
                }  
             else
              postfix = "AM";
    
        printf("The time is %.2d:%.2d %s \n", hour, min, postfix);
    
         return 0;
    }
    Last edited by CommonTater; 12-21-2011 at 03:21 AM.

  13. #13
    Registered User
    Join Date
    Oct 2011
    Location
    Raymond Terrace, New South Wales, Australia
    Posts
    7
    Quote Originally Posted by whiteflags View Post
    Fully brace all your code until you understand what not bracing means.
    My Only issue is, is that I am bracing in line with what the text is saying. I bracketed the if to try and avoid a "dangling" else.

    Edit: Am I reading the "right" book?
    Last edited by Sayth; 12-21-2011 at 03:21 AM.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sayth
    My Only issue is, is that I am bracing in line with what the text is saying. I bracketed the if to try and avoid a "dangling" else.
    What do you mean by "bracketed the if"? Working from CommonTater's fix in post #12 to your code, whiteflags means something like this:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int hour, min, myhour;
        const char *postfix;
    
        printf("Print the time in 24hour format: ");
        scanf("%d:%d", &hour, &min);
    
        if ((hour < 25 && hour > 0) && (min < 61 && min > 0))
        {
            if (hour > 12)
            {
                myhour = (hour - 12);
                postfix = "PM";
            }
            else
            {
                myhour = hour;
                postfix = "AM";
            }
            printf("The time is %.2d:%.2d %s \n", myhour, min, postfix);
        }
        else
        {
            printf("Your input was invalid \n");
        }
    
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Registered User
    Join Date
    Oct 2011
    Location
    Raymond Terrace, New South Wales, Australia
    Posts
    7
    Quote Originally Posted by laserlight View Post
    What do you mean by "bracketed the if"? Working from CommonTater's fix in post #12 to your code, whiteflags means something like this:
    I was referring to p82 of the book. The author doesn't use bracketing with the if's as in CommonTater's code.

    He advises to make sure that if you use two if's that you should bracket the second if to make sure that the else remains in the outer block.
    So:
    Code:
    if ( ) ... {
                   if ( ) ...
                       do  something }
    else
         something else

Popular pages Recent additions subscribe to a feed