In the User defined function sum(), i am getting the Lvalue required error, can you please tell me why?

``````#include<iostream.h>
#include<conio.h>
#include<math.h>

int num,digit,sum1,p;
int sqlarge(int &a, int &b, int t=0)
{
if(a>b)
{
t=a;
a=pow(t,2);
return a; }
if(a<b)
{
t=b;
b=pow(t,2);
return b; }
else
{cout<<"Both values can never be equal"<<endl;
return NULL; }
}

int sum(int a)
{

do
{
digit=a%10;
num=a/10;
p=digit+num;
sum1++=p;
}
while(num !=0);
return sum1;
}

int main()
{
clrscr();
int a,b,n;
cout<<"Enter two values"<<endl;
cin>>a>>b;
cout<<"Square of the larger value is ";
n=sqlarge(a,b);
cout<<n;
cout<<"Sum of the digits of squared larger value is ";
cout<<sum(n);
getch();
return 0;
}``````
4
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by PrimePackster

No. We have a 1 in 50 chance of picking the correct line the error is on.

WaltP is being (a bit) sarcastic (OP did indicate problem was in the sum() function), but he has a point. Many IDE programs will help you narrow down the problem line by indicating what line they think the problem is in, and if yours doesn't, then commenting out the lines one by one from within the sum() function should narrow down the options pretty fast. The ability to debug your programs is at least as important as figuring out how to write the code itself. Learning effective strategies on how to do so will serve you well in the future. Repost if you can get identify the problem line but don't know what's wrong, or how to fix it, or why.

Edited by Lerner: n/a

Well. Maybe WaltP has 1 in 50 chance of finding the error, but I have an eagle eye and a compiler built into my brain, which means I have a 100% chance of finding the error. And the error is at this line in your loop:

``sum1++=p;``

You have one too many + signs. The above line translates into:

``sum1++ = p;``

Which means that the variable sum1 is incremented by 1 and then, its old value is returned as a temporary value (formally, an "rvalue") which you then attempt to assign a value too (the value of p). Because of the nature of an rvalue (a temporary value) there is no way that you can assign another value to it, i.e., you can only assign a value to a non-temporary variable (formally, an "lvalue"). And thus, you get a compilation error like "lvalue required on the left side of the assignment".

I believe you meant to write:

``sum1 += p;``
Thanks Man, You saved my day
Good for you. Although my mini-teaching-session is now for naught. There's actually a reason I posted as I did.

Well. Maybe WaltP has 1 in 50 chance of finding the error, but I have an eagle eye and a compiler built into my brain, which means I have a 100% chance of finding the error. And the error is at this line in your loop:

``sum1++=p;``

You have one too many + signs. The above line translates into:

``sum1++ = p;``

Which means that the variable sum1 is incremented by 1 and then, its old value is returned as a temporary value (formally, an "rvalue") which you then attempt to assign a value too (the value of p). Because of the nature of an rvalue (a temporary value) there is no way that you can assign another value to it, i.e., you can only assign a value to a non-temporary variable (formally, an "lvalue"). And thus, you get a compilation error like "lvalue required on the left side of the assignment".

I believe you meant to write:

``sum1 += p;``

Thanks Man, You saved my day. Infact i never noticed that extra increment operator..Well even though the Lvalue error was rectified, there was a problem with the loop. Anyway i found it myself....