CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

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

1. Junior Member
Join Date
Feb 2016
Posts
8

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

hi to all,
I use Visual c++ 6.00
I have a code which in my opinion should work:
.....
CString one=" some text Q the rest of the text ";
CString two;
int x, y;
x=y=0;
while (one [x]!='Q')
two [y]=one[x]; // it gives error at this line: error C2106: '=' : left operand must be l-value
....
why don't it work?
Thanks
Val

2. Junior Member
Join Date
Feb 2016
Posts
8

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

sorry to all
I found a mistake in code, it should be:

.....
CString one=" some text Q the rest of the text ";
CString two;
int x, y;
x=y=0;
while (one [x]!='Q')
{
two [y]=one[x]; // it gives error at this line: error C2106: '=' : left operand must be l-value
x+=1;
y+=1;
}
....

but it still will give the same error....

3. Member
Join Date
Jan 2012
Location
India
Posts
193

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

use either
char two[50]
or
CString * two and use new .. delete.

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

CString[] only allows the value of a char to be obtained. It cannot be used to change. It can be used on right of = only and not on left side. See https://msdn.microsoft.com/en-us/lib...=vs.60%29.aspx

If you want to change you need to use SetAt(). See https://msdn.microsoft.com/en-us/lib...=vs.60%29.aspx

5. Elite Member Power Poster
Join Date
Aug 2000
Location
West Virginia
Posts
7,716

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

Since the variable, two, has a length of zero, I think that you should use operator +=

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

Originally Posted by Philip Nicoletti
Since the variable, two, has a length of zero, I think that you should use operator +=
Exactly!
Using CString::SetAt method could (in OP's context) lead to the application crash because of access violation!

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

I didn't properly read the code ! However, if the requirement is that two should contain the contents of one upto but not including the Q then consider using .Find() and .Left().

Code:
#include <afx.h>
#include <iostream>
using namespace std;

int main()
{
CString one = " some text Q the rest of the text ";

int f = one.Find('Q');
CString two = (f < 0) ? one : one.Left(f);

cout << two << endl;
}
Last edited by 2kaud; February 25th, 2016 at 08:23 AM.

8. Elite Member Power Poster
Join Date
Aug 2000
Location
West Virginia
Posts
7,716

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

Take a look at your code in post # 2, and consider what would happen if the variable one did not have a 'Q' in it (even assuming you used +=). 2kaud suggestion of using find bypasses these types of problems.

9. Junior Member
Join Date
Feb 2016
Posts
8

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

Hi to all,
Thank to all of you, that was a great help for me
It finally worked in this way:
........
while (one [x]!='Q')
{
two+=1;
two .SetAt(y,one[x]);
x+=1;
y+=1;
}
........
works – happy days
VictorN was right it crashed at the start, but when I added // two+=1;
It worked
Thanks to Philip for his advise...
I am searching a string which would defiantly have the character I am looking for.
The part of the string I need is in the middle of the cstring one this is why I didn't use // one.Find('Q')
...............
again thanks a lot to all for the your help...

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

I am searching a string which would defiantly have the character I am looking for.
It is still good practice to deal with the case where it doesn't.

If you want it coded using a while loop then you are appending 1 to the string and then using SetAt to change this to the required char! Consider
Code:
#include <afx.h>
#include <iostream>
using namespace std;

int main()
{
CString one = " some text Q the rest of the text ";
CString two;

for (int x = 0; (x < one.GetLength()) && (one[x] != 'Q'); ++x)
two += one[x];

cout << two << endl;
}
The part of the string I need is in the middle of the cstring one this is why I didn't use // one.Find('Q')
So why are you setting two to the first part of one? If you want to extract part of string one then use .Find() and .Mid() (or .Right()). What part of the string do you actually want?

11. Junior Member
Join Date
Feb 2016
Posts
8

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

Originally Posted by 2kaud
It is still good practice to deal with the case where it doesn't.

If you want it coded using a while loop then you are appending 1 to the string and then using SetAt to change this to the required char! Consider
Code:
#include <afx.h>
#include <iostream>
using namespace std;

int main()
{
CString one = " some text Q the rest of the text ";
CString two;

for (int x = 0; (x < one.GetLength()) && (one[x] != 'Q'); ++x)
two += one[x];

cout << two << endl;
}

So why are you setting two to the first part of one? If you want to extract part of string one then use .Find() and .Mid() (or .Right()). What part of the string do you actually want?
Finally I have used the solution you mention and it worked... the string I am working is a path string, something as: C:\Users\Public\Documents\some_folder\text.txt
so what I need the part "some_folder" extracted and been a CString
I have worked out how detect "\" before "some_folder" part, so what I needed was to copy everything before the next "\"...
All worked, I mean I was able to detect where to start copping and where to stop....
the actual copping the part of the CString one to CString two didn't work...
But with the help from this site I was able to do it in two methods:
1) using SetAt() function
2) char x=one[n];
two+=x;

both methods work, I kept the second one...
so, at the start I had two problems:
1) two[y]=one[x] // never works
2) CString two; // is empty string and it should not be empty

thanks for the help
regards
Val

12. Junior Member
Join Date
Feb 2016
Posts
8

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

I agree that it is good to have some code detecting the situation where it will be no 'Q' in the string.... It can't happen to my program, but I will add some procedure to detect that anywhere...
Thanks
Val

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

the string I am working is a path string, something as: C:\Users\Public\Documents\some_folder\text.txt
so what I need the part "some_folder" extracted and been a CString
Code:
#include <afx.h>
#include <iostream>
using namespace std;

int main()
{
CString one = "C:\\Users\\Public\\Documents\\some_folder\\text.txt";
int last = one.ReverseFind('\\');
int f1 = 0;

for (int f = 0; (f = one.Find('\\', f1)) != last; f1 = f + 1);

CString two = (last >= 0) ? one.Mid(f1, last - f1) : one;

cout << two << endl;

}
This displays
Code:
some_folder

14. Elite Member Power Poster
Join Date
Nov 2003
Location
Florida
Posts
12,559

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

Originally Posted by Lvalera
Finally I have used the solution you mention and it worked... the string I am working is a path string, something as: C:\Users\Public\Documents\some_folder\text.txt
so what I need the part "some_folder" extracted and been a CString
I have worked out how detect "\" before "some_folder" part, so what I needed was to copy everything before the next "\"...
All worked, I mean I was able to detect where to start copping and where to stop....
the actual copping the part of the CString one to CString two didn't work...
But with the help from this site I was able to do it in two methods:
1) using SetAt() function
2) char x=one[n];
two+=x;

both methods work, I kept the second one...
so, at the start I had two problems:
1) two[y]=one[x] // never works
2) CString two; // is empty string and it should not be empty

thanks for the help
regards
Val
Why not use CString::Mid to extract the portion you want?

15. Junior Member
Join Date
Feb 2016
Posts
8

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

Originally Posted by GCDEF
Why not use CString::Mid to extract the portion you want?
I will try,
thanks a lot...

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•