Thread: error: lvalue required, pretty sure it's an lvalue

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by anduril462
    Your rewrite of the expression in post #5 always returns x (due to the GNU extension with the empty true clause), regardless of whether x/(2^y) == 0.
    That is not true, because my rewrite does not compile, which is precisely the reason why I wrote it: to demonstrate what the expression would be like if parentheses were used to explicitly group, and hence explain why brandones encountered the "lvalue required" error.
    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

  2. #17
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by laserlight View Post
    That is not true, because my rewrite does not compile, which is precisely the reason why I wrote it: to demonstrate what the expression would be like if parentheses were used to explicitly group, and hence explain why brandones encountered the "lvalue required" error.
    Ahh, that makes sense. Thanks for all the clarification.

  3. #18
    Registered User
    Join Date
    Aug 2011
    Posts
    2

    Thank you!

    Got the code working. There were quite a few problems, including some basic mathematical ones. Thank you for the help!

    Code:
    	printf("%d", x<0);
    	x<0 ? (x=-x) : x;
    	for(y=14; y>-1; y--){
    		int z = x-(pow(2, y));
    		z<0 ? printf("0") : printf("1");
    		x = (z<0 ? x : z);

  4. #19
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by brandones View Post
    Got the code working. There were quite a few problems, including some basic mathematical ones. Thank you for the help!

    Code:
    	printf("%d", x<0);
    	x<0 ? (x=-x) : x;
    	for(y=14; y>-1; y--){
    		int z = x-(pow(2, y));
    		z<0 ? printf("0") : printf("1");
    		x = (z<0 ? x : z);
    Congrats on getting it working... Here's a complete solution in 20 lines...

    Code:
    #include <stdio.h>
    
    int main (void)
      { char binary[33] = {0};
        unsigned int number;
        int x = 0;
    
        printf("Enter a number from 0 to 4294967295 : ");
        scanf("%u", &number);
    
        while(x < 32)
          binary[x++] = '0';
        do
          binary[--x] = (number & 1) + '0';
        while ( (number /= 2) > 0 );
    
        printf("32bit binary :  %s\n\n", binary);
    
        return 0; }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: lvalue required as left operand of assignment
    By owjian1987 in forum C Programming
    Replies: 5
    Last Post: 02-11-2011, 12:34 PM
  2. Replies: 3
    Last Post: 06-01-2010, 06:22 AM
  3. Lvalue required error
    By Dink87522 in forum C Programming
    Replies: 4
    Last Post: 10-04-2009, 08:34 PM
  4. Lvalue required error
    By eklavya8 in forum C Programming
    Replies: 5
    Last Post: 01-03-2009, 04:47 PM
  5. Lvalue required --compile error--
    By cecu in forum C++ Programming
    Replies: 2
    Last Post: 05-19-2006, 07:40 PM