Thread: error C2106: '=' : left operand must be l-value

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    error C2106: '=' : left operand must be l-value

    Hi,

    The following code generates the error:"error C2106: '=' : left operand must be l-value"

    Code:
    // This is the main project file for VC++ application project 
    // generated using an Application Wizard.
    
    #include "stdafx.h"
    #include "stdio.h"
    #using <mscorlib.dll>
    using namespace System;
    
    
    #define IN 1 /* inside a word */
    #define OUT 0 /* outside a word */
    /* count lines, words, and characters in input */
    
    main()
    {
    int c, nl, nw, nc, state;
    state = OUT;
    nl = nw = nc = 0;
    while ((c = getchar()) != EOF) {
    ++nc;
    if (c == '\n')
    ++nl;
    if (c == ' ' || c == '\n' || c = '\t')
    state = OUT;
    else if (state == OUT) {
    state = IN;
    ++nw;
    }
    }
    printf("%d %d %d\n", nl, nw, nc);
    }
    Can you tell me what is the reason?
    Thanks!!

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    1: Learn to indent your code properly.
    2: Which line is it complaining about, I'm to busy to try to compile it myself.
    3: Should this be == instead of =?
    Code:
    if (c == ' ' || c == '\n' || c = '\t')

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    using namespace System;
    is this C?

    you need to indent properly
    and show us the line that caused the error
    also note that c = '\t' should be c == '\t'
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Totally unreadable code. This code will get nowhere. If I'd grate you from A to F, you'd get G.
    http://cpwiki.sourceforge.net/User:Elysia/Indentation
    Last edited by Elysia; 02-06-2008 at 10:42 AM.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User aLiNuSh's Avatar
    Join Date
    Aug 2007
    Location
    127.0.0.1
    Posts
    7
    If you'd give us the line number where the error occurred maybe we could help more...
    Until then there's definitely something wrong here
    Code:
    if (c == ' ' || c == '\n' || c = '\t')
    I'm sure you meant...
    Code:
    if (c == ' ' || c == '\n' || c == '\t')
    I'm not sure if that generated the error, I remember Borland C++ 3.1, which is a very old compiler, used to generate errors like that when assigning values in if statements.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by aLiNuSh View Post
    If you'd give us the line number where the error occurred maybe we could help more...
    Until then there's definitely something wrong here
    Code:
    if (c == ' ' || c == '\n' || c = '\t')
    I'm sure you meant...
    Code:
    if (c == ' ' || c == '\n' || c == '\t')
    I'm not sure if that generated the error, I remember Borland C++ 3.1, which is a very old compiler, used to generate errors like that when assigning values in if statements.
    I'm pretty sure that's the problem. (c == ' ' || c == '\n' || c) is not an lvalue (an lvalue is "something that can be assigned a value", so a simple variable, one cell of an array or a dereferences pointer).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    And, anyway . . .
    Code:
    if (c == ' ' || c == '\n' || c == '\t')
    Consider isspace() from <ctype.h>. It does basically just that, except more portably. It also counts vertical tabs ('\v') and carriage returns ('\r') as whitespace in the standard C locale, and whatever else might count as "whitespace" in any other locale the user might be using.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    Totally unreadable code. This code will get nowhere. If I'd grate you from A to F, you'd get G.
    Then you would not make it far as a grader.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    'G' as in Good!

    If the grading was based purely on indentation, I can't say it would be very favourable, however . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    More like G as in garbage.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    More like G as in garbage.
    Blah blah blah.

    It takes a lot of experience (years) to completely understand the value of indentation. This is why so many beginners have poor indentation. It's not their fault. Telling a beginner "Remember to indent properly" is just about as memorable as telling somebody "Remember to scratch directly behind your left ear before descending the staircase." It might be important, but without some context it isn't going to stick.

    You can make all sorts of arguments why indentation is important. The beginner will rightfully say, "So what?" This is because of a lack of experience, not stupidity.

    If you want to fail (or, in your case, "worse than fail") introductory students because their indentation sucks, you will find your classes mysteriously empty of students. Nobody wants to learn from a psycho like that.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It amazes me that people even write code with no a single indent at all. How can they even read such code?
    Yes, I'm cranky today.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Elysia View Post
    It amazes me that people even write code with no a single indent at all. How can they even read such code?
    That question assumes they can read even properly indented code. Most beginners deal with a single line at a time.

    Experienced programmers should push the importance of indentation but also remain patient as beginners slowly realize why.

  14. #14
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > It takes a lot of experience (years) to completely understand the value of indentation. This is why so many
    > beginners have poor indentation. It's not their fault.

    I always indented properly, from the beginning. It was obvious to me that it's hard to get the logic right if one doesn't bother to write the code so it's clear which nesting level one is in.

    > Nobody wants to learn from a psycho like that.

    There are automatic indenting tools available, so even if the student doesn't feel like indenting properly as they go, they can easily take care of it afterwards if they're told how.

  15. #15
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by robatino View Post
    I always indented properly, from the beginning.
    And some people played the piano at age 2.

    There are automatic indenting tools available, so even if the student doesn't feel like indenting properly as they go, they can easily take care of it afterwards if they're told how.
    Indentation should be taught. I object to the idea of "failing" students for bad indentation at beginning levels. Are we deliberately trying to discourage people from persevering?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Odd 3D Invis Objects?
    By Zeusbwr in forum Game Programming
    Replies: 4
    Last Post: 12-07-2004, 07:01 PM
  2. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  3. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 04:49 AM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM