what is the =' : left operand must be l-value means?

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

#include <iostream>   // std::cout
#include <fstream>
#include <iomanip>
#include <string>    // std::string
#include <vector>    // std::vector<>
#include <algorithm> //std::for each()
using namespace std; // import "std" namespace into global namespace

struct exam
{
int examid;
vector <int> total;
};

void SwapMembers (int items[], int index1, int index2)
{
	int temp;
	temp=items[index1];
	items[index1]=items[index2];
	items[index2]=temp;
}

	int main() 
	{
	ifstream stream1("STA83SOLUTION.txt");
		if ( !stream1.is_open())
		{
		cout << "While opening a file an error is encountered" << endl;
		} 
			else 
			{
			cout << "Fail Di buka....." << endl;
			}
	vector <exam> exams;
	exam aExam;
   int tempExamID;
    int tempTotal;
    stream1 >> tempExamID >> tempTotal;
    aExam.examid = tempExamID;
    aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
    while (stream1 >> tempExamID >> tempTotal)
    {
        if(tempExamID != aExam.examid)
        {
        exams.push_back(aExam); // no more exam codes for this student.  Add aStudent to students vector
        aExam.total.clear();
        aExam.examid = tempExamID;
        }
        aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
    }
    exams.push_back(aExam); // no more exam codes for this student.  Add aStudent to students vector
    stream1.close(); // We have read the entire file, so time to close it.
{
	ofstream myfile;
	myfile.open("411.txt");
	
	int temp, flag;
	if (myfile.is_open())
	{
		for (size_t i = 0; i < exams.size(); i++) 
		{
			for (size_t j = 0; j<exams.at(i).total.size(); j++) 
			{
				if (exams.at(i).total.size() < exams.at(i).total.size()+1)      // ascending order simply changes to <
				{ 
                    temp = exams.at(i).total.at(j);             // swap elements
                    exams.at(i).total.at(j) = exams.at(i).total.at(j)+1;
                    exams.at(i).total.at(j)+1 = temp;
                    flag = 1;               // indicates that a swap occurred.
                }

		cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
	  	cout<<" "<< exams.at (i).total.at(j)<<"\t";
			}
		}
	}
					
cin.get();
return 0;
}
}

Dani AI

Generated

The compiler error C2106 ("left operand must be l-value") means the left side of = must name a modifiable storage location. In this thread the code tries to assign to the result of an expression (an element plus 1) instead of to an actual element. That expression is a temporary (an rvalue), so the assignment is illegal. As pointed out, the correct intent is to refer to the other element (index j+1), not to add +1 to the element itself.

Practical fixes and improvements:

  • Index the next element (use j+1 inside the index) and fix the inner-loop bound so j+1 never runs past the end (j + 1 < list.size()).
  • Prefer std::swap or std::iter_swap instead of manual temp code.
  • If sorting the small vectors is the goal, prefer the standard algorithm std::sort(list.begin(), list.end()); (or std::stable_sort if stability matters).

Example pattern (different style from earlier posts):

auto &list = exams[i].total;
if (list.size() > 1) {
    bool changed;
    do {
        changed = false;
        for (size_t j = 0; j + 1 < list.size(); ++j) {
            if (list[j] > list[j+1]) {
                std::swap(list[j], list[j+1]);
                changed = true;
            }
        }
    } while (changed);
}

Other notes based on the thread: the SwapMembers function in the code accepts a C array but is unused—either remove it or adapt it to accept vector<int>&. The first read from the file is done without checking success (an empty file will leave aExam in an inconsistent state). Also the code opens myfile but prints to cout; write to myfile if the intent is to save output. These fixes address the immediate compiler complaint and improve robustness.

Recommended Answers

All 6 Replies

yesss..but i dont understand the link that you give.

Short version - the item on the left side of the assignment operator ( = ) must be something which may be modified - a single variable. Left side => l_value. So, you cannot write something like 12 = x * y; or int arr[10]; arr = 12; Now go back and look at your line 68 above.

So, I must change like this?????

temp = exams.at(i).total.at(j);             // swap elements
                    exams.at(i).total.at(j) = exams.at(i).total.at(j+1);
                    temp = exams.at(i).total.at(j+1);
                    flag = 1;

Almost. The problem you had was putting the +1 outside the parenthesis.
So, your code should be:

temp = exams.at(i).total.at(j);             // swap elements
exams.at(i).total.at(j) = exams.at(i).total.at(j+1);
exams.at(i).total.at(j+1) = temp;
flag = 1;

i do like this but abnormal

#include <iostream>   // std::cout
#include <fstream>
#include <iomanip>
#include <string>    // std::string
#include <vector>    // std::vector<>
#include <algorithm> //std::for each()
using namespace std; // import "std" namespace into global namespace

struct exam
{
int examid;
vector <int> total;
};

void SwapMembers (int items[], int index1, int index2)
{
	int temp;
	temp=items[index1];
	items[index1]=items[index2];
	items[index2]=temp;
}

	int main() 
	{
	ifstream stream1("STA83SOLUTION.txt");
		if ( !stream1.is_open())
		{
		cout << "While opening a file an error is encountered" << endl;
		} 
			else 
			{
			cout << "Fail Di buka....." << endl;
			}
	vector <exam> exams;
	exam aExam;
   int tempExamID;
    int tempTotal;
    stream1 >> tempExamID >> tempTotal;
    aExam.examid = tempExamID;
    aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
    while (stream1 >> tempExamID >> tempTotal)
    {
        if(tempExamID != aExam.examid)
        {
        exams.push_back(aExam); // no more exam codes for this student.  Add aStudent to students vector
        aExam.total.clear();
        aExam.examid = tempExamID;
        }
        aExam.total.push_back(tempTotal); // add this exam code to current student's vector of exam codes
    }
    exams.push_back(aExam); // no more exam codes for this student.  Add aStudent to students vector
    stream1.close(); // We have read the entire file, so time to close it.
{
	ofstream myfile;
	myfile.open("411.txt");
	
	int temp, flag;
	if (myfile.is_open())
	{
		for (size_t i = 0; i < exams.size(); i++) 
		{
			flag=0;
			for (size_t j = 0; j<exams.at(i).total.size(); j++) 
			{
				if (exams.at(i).total.at(j+1) > exams.at(i).total.at(j) )     // ascending order simply changes to <
				{ 
                   temp=exams.at(i).total.at(j)   ;             // swap elements
                    exams.at(i).total.at(j) = exams.at(i).total.at(j+1);
                    exams.at(i).total.at(j+1) = temp;
                    flag = 1;               // indicates that a swap occurred.
                }

		cout<<"\n"<<i+1<<":"<<" "<< exams.at (i).total.at(j)<<"\t"; // output list of exam codes for this student
	  	cout<<" "<< exams.at (i).total.at(j)<<"\t";
			}
		}
	}

					
cin.get();
return 0;
}
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.