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?
Could anyone help me please
Thanks
Val
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....
Re: error C2106: '=' : left operand must be l-value
use either
char two[50]
or
CString * two and use new .. delete.
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
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 +=
Re: error C2106: '=' : left operand must be l-value
Quote:
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!
Re: error C2106: '=' : left operand must be l-value
I didn't properly read the code ! :blush: 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;
}
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.
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...
Re: error C2106: '=' : left operand must be l-value
Quote:
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;
}
Quote:
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?
Re: error C2106: '=' : left operand must be l-value
Quote:
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
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
Re: error C2106: '=' : left operand must be l-value
Quote:
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
So why not ask this question to start with? Consider
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
Re: error C2106: '=' : left operand must be l-value
Quote:
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?
Re: error C2106: '=' : left operand must be l-value
Quote:
Originally Posted by
GCDEF
Why not use CString::Mid to extract the portion you want?
I will try,
thanks a lot...