I am just starting to teach myself C++ and i have no coding experience so please bear with me on this if I ask you to explain your reasoning behind your response.

My code is:

// operating with varables
#include <iostream>
using namespace std;

//declarations
char STAR;
int PRIME = 71;

//begin main body
int main ()
{
    int count, sum, ex, num;
    sum = count + PRIME; 
    num = count * 1 + 2;
    (sum + count = ex);
    ex = sum * count;
    count = 25.67;
    cout << " Count = " << count << ", Sum = " << sum << ", Prime = " << PRIME << endl;
    return 0;

}

Debug errors received:

Error   1   error C2106: '=' : left operand must be l-value (error on line in bold)
Warning 2   warning C4244: '=' : conversion from 'double' to 'int', possible loss of data

Let me know what you all think.

Recommended Answers

All 4 Replies

How about:

ex=(sum+count);

?

Also the value 25.67 has a fractional part and an integer does not.
If you want the value to keep the .67, you will need to make the count a double (or other type of floating point number).

The warning you got means the framework will convert that double to an integer and it will lose the fractional part.

Error 1 error C2106: '=' : left operand must be l-value (error on line in bold)

Did you mean to put the value you get after summing sum and count into ex. If so, you should do like this.

ex = sum + count;

Warning 2 warning C4244: '=' : conversion from 'double' to 'int', possible loss of data

count is declared as an int, which means it can hold values like 100, 101, 102... but not 101.43 or 3.14. But here you are trying to assign 25.67 to count. This is not an error, but eventually your count will ignore 0.67 and store only 25. So, if the 0.67 is important in your case you should use data types like double or float for count

I am just starting to teach myself C++ and i have no coding experience so please bear with me on this if I ask you to explain your reasoning behind your response.

My code is:

>     // operating with varables
>     #include <iostream>
>     using namespace std;
>     
>     //declarations
>     char STAR;
>     int PRIME = 71;
>     
>     //begin main body
>     int main ()
>     {
>       int count, sum, ex, num;
>       sum = count + PRIME; 
>       num = count * 1 + 2;
>       (sum + count = ex);
>       ex = sum * count;
>       count = 25.67;
>       cout << " Count = " << count << ", Sum = " << sum << ", Prime = " << PRIME << endl;
>       return 0;
>     
>     }
Debug errors received:
Error 1   error C2106: '=' : left operand must be l-value (error on line in bold)
Warning   2   warning C4244: '=' : conversion from 'double' to 'int', possible loss of data

Let me know what you all think.

Keep in mind that the "=" symbol is an assignment operator - NOT an equals sign. For example, if you write: x = 10; do not get into the habit of saying x equals 10! That is not what's happening. In C++, this is a valid statement: x = x + 1; but mathematically that would be incorrect, since x cannot be equal to itself PLUS one more. Always say, "x is assigned the value of 10", or "x gets 10". It will mess you up later if you always think of the "=" sign as an equals sign - it's not, it is an assignment operator.

Any variable, such as count or ex or sum, is a name for a memory location. If we say, "x = 10;" then we are saying "Place the value of 10 into the memory location that we call x". So any variable is a container of sorts that can hold something, and obviously that is what is required on the left hand side of an assignment operator, right? An l-value is just that, a container of sorts (memory location) that can hold a value, and that must be what is placed on the left side of an assignment operator.

Imagine that you had 3 bank accounts, and they were named sum, count and ex. And you had $50 in each account. Now, if you said to the bank teller: sum + count = ex then in essence you would be saying to transfer the $50 in your ex account and place it into your sum + count account. ??? That doesn't make sense - left operand must be an l-value! But, if you said ex = sum + count then you would be saying "Take the $50 from my sum account and the $50 from my count account and place both in my ex account. Now that makes sense, since ex is a proper l-value, ie., one that can hold a value. Sum + count cannot hold a value, it is not a memory location, but rather is just a temporary value that is used in the middle of a calculation, and is just transient in nature. (Sum + count) is an r-value, one that goes on the right side of an assignment operator. R-values are not containers of any sort, like a memory location, but rather are just temporary values that come up in the middle of a calculation, which are soon discarded. So r-values generally go into l-values, but you had them reversed.

Just a note for clarification: ex = sum + count - here (sum + count) replaces the value in ex, it isn't added to the existing contents of ex.

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.