main.cpp:47:55: error: lvalue required as left operand of assignment

Write your question here.

Need to compare the sales value of two sales teams in a company. Then need to assign a score by comparing those to figures monthly. First i ask the user to enter number of moths to enter. Then ask the user to feed the sales values for each team. Then to compare the values of each month and assign a socre. But i get below error. pls help
-----------
main.cpp: In function ‘int main()’:
main.cpp:47:55: error: lvalue required as left operand of assignment
if (a[0][x]>a[1][y]) {cin>>a[2][x]=P>>a[3][y]=Q;}
^
main.cpp:48:61: error: lvalue required as left operand of assignment
else if (a[0][x]==a[1][y]) {cin>>a[2][x]=R>>a[3][y]=R;}
^
main.cpp:49:39: error: lvalue required as left operand of assignment
else {cin>>a[2][x]=Q>>a[3][y]=P;}
^

---------------------------------------




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

int main()
{
   
   
    int a[4][12]; /*array to save data */
    int n=0;
    
    
    
    cout<<"Enter number of periods"<< std::endl;
    cin>>n;
    cout<<"Enter scores of team A"<< std::endl;
    
    for(int x=0 ; x<n; x++)
    
    
    cin>>a[0][x];
    for(int x=0 ; x<n; x++)
    cout<<a[0][x]<<endl;
    
    cout<<"Enter scores of team B"<< std::endl;
    for(int y=0 ; y<n; y++)
    cin>>a[1][y];
    for(int y=0 ; y<n; y++)
    cout<<a[1][y]<<endl;
    
    
    
    int P = 4;
    int Q = 2;
    int R = 3;

    for(int x=0, y=0; x<n ; x++, y++)

        if (a[0][x]>a[1][y]) {cin>>a[2][x]=P>>a[3][y]=Q;}
        else if (a[0][x]==a[1][y]) {cin>>a[2][x]=R>>a[3][y]=R;}
        else {cin>>a[2][x]=Q>>a[3][y]=P;}
   
  
   
}
cin>>a[2][x]=P>>a[3][y]=Q;

This makes no sense.

This bit is ok:
cin>>a[2][x]

That bit where you suddenly say =P

That makes no sense. What are you trying to set to the value of P?

Also, see this:

1
2
3
4
5
    for(int x=0, y=0; x<n ; x++, y++)

        if (a[0][x]>a[1][y]) {cin>>a[2][x]=P>>a[3][y]=Q;}
        else if (a[0][x]==a[1][y]) {cin>>a[2][x]=R>>a[3][y]=R;}
        else {cin>>a[2][x]=Q>>a[3][y]=P;}

Seriously, just put braces around your for-loop body.

1
2
3
4
5
6
for(int x=0, y=0; x<n ; x++, y++)
{
        if (a[0][x]>a[1][y]) {cin>>a[2][x]=P>>a[3][y]=Q;}
        else if (a[0][x]==a[1][y]) {cin>>a[2][x]=R>>a[3][y]=R;}
        else {cin>>a[2][x]=Q>>a[3][y]=P;}
}
Last edited on
Hi , P is the score for the highest amount . That is 4 points . Other team should get only 2.

Based on above i assign values to two arrays .

cin>>a[2][x]=P>>a[3][y]=Q;

in every cycle values in row 0 and row 1 is compared and assign the score for row 2 and 3. Row two is allocated to store the score of row 0 ( Team 1). Row three is for team 2.
in every cycle values in row 0 and row 1 is compared and assign the score for row 2 and 3.

That may be what you think should happen, but since the compiler disagrees you're going to need to do something different.

What makes you think that doing an assignment in a cin statement makes sense?

Perhaps you need to remove the "cin >>" nonsense and just do the assignments?

1
2
3
4
5
if (a[0][x] > a[1][y]) 
{
   a[2][x] = P;
   a[3][y] = Q;
}
Last edited on
Based on above i assign values to two arrays .

cin>>a[2][x]=P>>a[3][y]=Q;


As JLB says, what you wrote JUST ISN'T C++ CODE.

You're just writing nonsense. It might make sense to you, but it's not C++ code.
Hello hekanayake,



Based on above i assign values to two arrays .

cin>>a[2][x]=P>>a[3][y]=Q;


Where is the other array?

First start with fixing the code so it is easier to read:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <limits>

int main()
{
    constexpr int MAXROWS{ 4 }, MAXCOLS{ 12 };
    //constexpr int MAXSCORE{ ? };  // <--- Replace ? with highest possible scroe.

    int teamScores[MAXROWS][MAXCOLS]{}; /*array to save data */
    int numOfPeriods{};

    do
    {
        std::cout << "Enter number of periods: ";
        std::cin >> numOfPeriods;

        if (!std::cin || numOfPeriods > MAXCOLS)
        {
            if (!std::cin)
            {
                std::cout << "\n     You must enter a number!\n\n";

                std::cin.clear();  // <--- Resets the state bits on the stream.
                std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>. Clears the input buffer.
            }
            else if (numOfPeriods > MAXCOLS)
            {
                std::cout << "\n     To many periods! Max is " << MAXCOLS << ".\n\n";
            }
        }

    } while (numOfPeriods > MAXCOLS || numOfPeriods == 0);

    std::cout << "\nEnter scores of team A\n";

    for (int col = 0, row{}; col < numOfPeriods; col++)
    {
        std::cout << " Enter score #" << col + 1 << ": ";
        std::cin >> teamScores[row][col];
    }

    //for (int x = 0; x < numOfPeriods; x++)  // <--- Good for testing, but not necessary otherwise.
    //    std::cout << teamScores[0][x] << ' ';
    
    //std::cout << std::endl;

    std::cout << "\nEnter scores of team B\n";

    for (int col = 0, row{ 1 }; col < numOfPeriods; col++)
    {
        std::cout << " Enter score #" << col + 1 << ": ";
        std::cin >> teamScores[row][col];
    }

    //for (int y = 0; y < numOfPeriods; y++)  // <--- Good for testing, but not necessary otherwise.
    //    std::cout << teamScores[1][y] << std::endl;

    int P{ 4 };  // <--- Should it be constexpr int P{ 4 }; ? Do you intend to have these variables change?
    int Q = 2;
    int R = 3;

    //for (int col = 0/*, y = 0*/; col < numOfPeriods; col++/*, y++*/)
    //    if (teamScores[0][col] > teamScores[1][y])
    //    {
    //        std::cin >> teamScores[2][col] = P >> teamScores[3][col] = Q;
    //    }
    //    else if (teamScores[0][col] == teamScores[1][col])
    //    {
    //        std::cin >> teamScores[2][col] = R >> teamScores[3][col] = R;
    //    }
    //    else
    //    {
    //        std::cin >> teamScores[2][col] = Q >> teamScores[3][col] = P;
    //    }

    return 0;  // <--- Not required, but makes teamScores good break point.
}

The code shows some concepts that you should fine useful.

A better use of blank lines along with a good variable name makes the code much easier to follow.

I removed the extra blank lines that are not needed and added some that are.

"main" starts with the constant variables. I find this makes it easier should they ever need to be changed.

Next it is always a good idea to initialize the variables. The empty {}s, the uniform initializer available from C++11 on, make this very easy even for the array.


The do/while loop allows for a valid entry and the if statement checks for anything that is incorrect.

The formatted input std::cin >> numOfPeriods; is expecting a number to be entered. If not "cin" will fail and will become unusable the rest of the program.

The do/while loop keeps asking until you enter a valid number. This is done to keep from going past the end of the array and to catch any problem with "cin".

From line 34 on try to use the new line (\n) instead of the "std::endl". You may not see it in a program this small, but in a large program the overhead of "endl" can cause the program to run slower.

The other thing you may not realize is:
1
2
std::cout << "Enter number of periods: ";
std::cin >> numOfPeriods;

There are 2 things here. First leaving out any (\n) or "endl" will cause the "cin" to be on the same line as the prompt. Tat is what the ": " is for. And second a "cout" followed by a "cin" the "cin" will flush the output buffer before it takes any input. At least as I understand it.

The section that is commented out is because, as you have been told, does not work and I do not have any idea what you want to do.

The code like std::cin >> teamScores[2][col] = P >> teamScores[3][col] = Q; you will need to explain what your intention is and what you want it to do because only you know what you need.

Andy
Topic archived. No new replies allowed.