CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2013
    Posts
    2

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

    Trying to show the Row totals and the Column totals.

    #include <stdio.h>

    int main()
    { /* start of main */
    #define NUMROWS 5 /* establishes NUMROWS as 5 */
    #define NUMCOLS 5 /* establishes NUMCOLS as 5 */
    int val[NUMROWS][NUMCOLS] = { {23,06,85,22,01}, /* declares array val to have 5 rows and 5 columns */
    {07,33,62,17,04},
    {89,71,60,54,33},
    {05,21,11,31,12},
    {61,25,02,04,69} }; /* initializes array val */

    printf("\nRow 0 %d %d %d %d %d", val[0][0], val[0][1], val[0][2], val[0][3], val[0][4]);
    printf("\nRow 1 %d %d %d %d %d", val[1][0], val[1][1], val[1][2], val[1][3], val[1][4]);
    printf("\nRow 2 %d %d %d %d %d", val[2][0], val[2][1], val[2][2], val[2][3], val[2][4]);
    printf("\nRow 3 %d %d %d %d %d", val[3][0], val[3][1], val[3][2], val[3][3], val[3][4]);
    printf("\nRow 4 %d %d %d %d %d", val[4][0], val[4][1], val[4][2], val[4][3], val[4][4]);


    int sumrow0, sumrow1, sumrow2, sumrow3, sumrow4; /* declares sumrow0, sumrow1, sumrow2, sumrow3, sumrow4 to be a int variable */
    sumrow0 = val[0][0] + val[0][1] = val[0][2] + val[0][3] + val[0][4];
    sumrow1 = val[1][0] + val[1][1] = val[1][2] + val[1][3] + val[1][4];
    sumrow2 = val[2][0] + val[2][1] = val[2][2] + val[2][3] + val[2][4];
    sumrow3 = val[3][0] + val[3][1] = val[3][2] + val[3][3] + val[3][4];
    sumrow4 = val[4][0] + val[4][1] = val[4][2] + val[4][3] + val[4][4];

    int sumcol0, sumcol1, sumcol2, sumcol3, sumcol4; /* declares sumcol0, sumcol1, sumcol2, sumcol3, sumcol4 to be a int variable */
    sumcol0 = val[0][0] + val[1][0] = val[2][0] + val[3][0] + val[4][0];
    sumcol1 = val[0][1] + val[1][1] = val[2][1] + val[3][1] + val[4][1];
    sumcol2 = val[0][2] + val[1][2] = val[2][2] + val[3][2] + val[4][2];
    sumcol3 = val[0][3] + val[1][3] = val[2][3] + val[3][3] + val[4][3];
    sumcol4 = val[0][4] + val[1][4] = val[2][4] + val[3][4] + val[4][4];


    #define ROWTOTALS 5 /* establishes ROWTOTALS containing 5 numbers */
    int row[ROWTOTALS] = { sumrow0, sumrow1, sumrow2, sumrow3, sumrow4 };

    #define COLTOTALS 5 /* establishes COLTOTALS containing 5 numbers */
    int col[COLTOTALS] = { sumcol0, sumcol1, sumcol2, sumcol3, sumcol4 };

    printf("\nRow totals: %d", row);
    printf("\nColumn totals: %d", col);

    return 0;
    }

    I receive this message after building:

    1>------ Build started: Project: WA 4 A 1 Christopher Caron, Configuration: Debug Win32 ------
    1> WA 4 A 1 Christopher Caron.cpp
    1>c:\users\chris\documents\visual studio 2010\projects\wa 4 christopher caron\wa 4 christopher caron\wa 4 a 1 christopher caron.cpp(21): error C2106: '=' : left operand must be l-value
    1>c:\users\chris\documents\visual studio 2010\projects\wa 4 christopher caron\wa 4 christopher caron\wa 4 a 1 christopher caron.cpp(22): error C2106: '=' : left operand must be l-value
    1>c:\users\chris\documents\visual studio 2010\projects\wa 4 christopher caron\wa 4 christopher caron\wa 4 a 1 christopher caron.cpp(23): error C2106: '=' : left operand must be l-value
    1>c:\users\chris\documents\visual studio 2010\projects\wa 4 christopher caron\wa 4 christopher caron\wa 4 a 1 christopher caron.cpp(24): error C2106: '=' : left operand must be l-value
    1>c:\users\chris\documents\visual studio 2010\projects\wa 4 christopher caron\wa 4 christopher caron\wa 4 a 1 christopher caron.cpp(25): error C2106: '=' : left operand must be l-value
    1>c:\users\chris\documents\visual studio 2010\projects\wa 4 christopher caron\wa 4 christopher caron\wa 4 a 1 christopher caron.cpp(28): error C2106: '=' : left operand must be l-value
    1>c:\users\chris\documents\visual studio 2010\projects\wa 4 christopher caron\wa 4 christopher caron\wa 4 a 1 christopher caron.cpp(29): error C2106: '=' : left operand must be l-value
    1>c:\users\chris\documents\visual studio 2010\projects\wa 4 christopher caron\wa 4 christopher caron\wa 4 a 1 christopher caron.cpp(30): error C2106: '=' : left operand must be l-value
    1>c:\users\chris\documents\visual studio 2010\projects\wa 4 christopher caron\wa 4 christopher caron\wa 4 a 1 christopher caron.cpp(31): error C2106: '=' : left operand must be l-value
    1>c:\users\chris\documents\visual studio 2010\projects\wa 4 christopher caron\wa 4 christopher caron\wa 4 a 1 christopher caron.cpp(32): error C2106: '=' : left operand must be l-value
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

    Any thoughts?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

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

    Quote Originally Posted by dedite View Post
    Code:
    	int sumrow0, sumrow1, sumrow2, sumrow3, sumrow4; /* declares sumrow0, sumrow1, sumrow2, sumrow3, sumrow4 to be a int variable */
    	sumrow0 = val[0][0] + val[0][1] = val[0][2] + val[0][3] + val[0][4];
    And what are you going to achieve with this strange assignment?
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

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

    Any thoughts?
    Please format your code before posting and use code tags (Go Advanced, select code, click '#').

    In addition to post #2, you will also have problems with

    Code:
    printf("\nRow totals: %d", row);
    printf("\nColumn totals: %d", col);
    as row and code are of type int* If you want to display the individual row and col totals you'll have to display for each one.

    You are also making heavy weather of doing this by hardcoding the sums. What happens if the number of cols or rows changes? It would be best to use for loops.

    As this looks like being an assignment, I can't give you the revised code as that would be cheating but once you get it to compile and try using for loops and re-post your code we'll be happy to comment.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

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

Featured