# C++ error: "Non-lvalue error in assignment"

#### EEMajor

Joined Aug 9, 2006
67
Greetings everyone,
I am thankful for any help that you may give me. I am working on a program that takes a trips start time and end time, then shaves 25% off the time it takes to complete the trip. However, while doing a static cast, I get the error of 'non-lvalue in assignment'. Everything else seems to work, at least from what I can tell. There are no other errors or warnings during an attempt to compile.
Here is my source code:
------------------------------------------------------------------------------------------------------------------------------------
#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{

int starttime = 0;
cout << "\nPlease enter the start time for the old trip in 24 hour format (hhmm): ";
cin >> starttime;
cin.get();

int endtime = 0;
cout << "\nPlease enter the end time for the old trip in 24 hour format (hhmm) :";
cin >> endtime;
cin.get();

// Declare integers c and pt for static casting later.
int newtime = 0;
int pt = 0;

//Get hours and minutes from the start time
float hours = endtime / 100 - starttime / 100;

float mins = endtime % 100 - starttime % 100;

float starthours = starttime/100;

float startmins = starttime%100;

// Take Minutes, divide by sixty to get hours(from minutes)
float minToHours = mins/60.0;

// Same as above, but with the starting minutes
float startMin = startmins/60.0;

// Get the total hours (from minutes to hours, and regular hours)
float added = minToHours + hours;

// Same as above, but with starting minutes and hours
float added1 = startMin + starthours;

float pretime = added1 + newhours;

float fmins = pretime - pt;

// Declare final numbers for the newtime calculation
float newHours = pt * 100;
float newMinutes = fmins * 60;
static_cast<int>(newtime) = newHours + newMinutes;

cout.fill ('0');
cout << "Your new ending time is: " << setw(4) << newtime << endl;

system("PAUSE");

return 0;
}
-------------------------------------------------------------------------------------------------------------------------------------------------

These are the two static casts I am having problems with:

static_cast<int>(newtime) = newHours + newMinutes;

Again, thank you for the help!

#### EEMajor

Joined Aug 9, 2006
67
Okay, I figured it out on my own after a good nights sleep and a little more research. I am posting the solution simply for anyone who may in the future be searching for similar information.

In my original source code I did this:

My problem is that the static_cast operator uses the parenthetical statement, and doesn't really listen to anything after that. So, the correct way to static cast the about red line is this way:

So, all in all a simple mistake. Gotta love that about programming, BE VERY carefull about what you type! It makes a difference!

DISCLAIMER: I am no programming expert by any stretch of the imagination. This information worked for me, with the MinGW Developer Studio. It may not work with a different compiler, or you may not even have the problem I did. I dunno!

#### n9352527

Joined Oct 14, 2005
1,198
As far as I know, static_cast take a value of any type and typecast it to a given type. It's similar to regular C typecast operator, but stricter. The way to use the static_cast is as follows:

cast_value = static_cast<type>(value);

For example:

double d = 15.4;
int i = static_cast<int>(d);

In your original code, instead of typecasting the value, you typecast the left hand value contained in the variable, which of course would do nothing value-wise because it is already an integer. It also throws the compiler off, because instead of treating the left hand statement as a variable in where the right hand value would be assigned, the static_cast actually convert the _value_ of the variable to an int and we all know that we can't assign the right hand value into another value. This is why the compiler came up with the non l-value error in assignment.

Your subsequent typecasting also not entirely accurate, instead of typecasting the r-value, you actually typecast the _entire_ statement, and do nothing with the typecasting result. However, the value contained in variable pt is correct, because the assignment actually cast the value automatically (when you assigned a float to an int variable). Depending on the compiler and warning/error settings, this should, however, throws a warning on assignment of incorrect type. See the example above for correct usage.

#### EEMajor

Joined Aug 9, 2006
67
n9352527,
Thank you very much for the information! I appreciate the response. That does make more sense, good to learn.

Thanks again.

#### alphaboy95

Joined Jul 21, 2008
1
excuse me but i received the same thing (Non-lvalue in assignment) when i was doing my homework.
here is the code :
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
int main ()
{
double a = 0.0;
double b = 0.0;
double c = 0.0;
double d1;
double d2;
double d3;
double x = 0;

cout << "Please input the 3 remainders.\n";

cin >> a;
cin >> b;
cin >> c;

3*x + a = d1;
5*x+b=d2;
7*x+c=d3;
while ( d1 != d2 != d3)
{
x++;
}

cout << ("Your number is", d1 ,".");
system ("PAUSE");
return 0;
}

#### Mark44

Joined Nov 26, 2007
628
These are the two static casts I am having problems with:

static_cast<int>(newtime) = newHours + newMinutes;
EEMajor, why do you think you need to use a cast of any kind? Your variables pt and newtime have been declared as type int, so you're casting a value of type int to type int.

Last edited:

#### Mark44

Joined Nov 26, 2007
628
excuse me but i received the same thing (Non-lvalue in assignment) when i was doing my homework.
here is the code :
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
int main ()
{
double a = 0.0;
double b = 0.0;
double c = 0.0;
double d1;
double d2;
double d3;
double x = 0;

cout << "Please input the 3 remainders.\n";

cin >> a;
cin >> b;
cin >> c;

3*x + a = d1; // <=== error here
5*x+b=d2; // <=== error here
7*x+c=d3; // <=== error here
while ( d1 != d2 != d3) // <=== possible error here
{
x++;
}

cout << ("Your number is", d1 ,"."); // I doubt that this will work
system ("PAUSE"); // I'm not sure, but I don't think this will work
return 0;
}

Alphaboy95,
You have a number of errors in this short program. The errors you mention about non lvalues come from the first three lines I marked as errors. An assignment statement has to have a variable (an lvalue) on the left side, and some value on the right side. The l in lvalue stands for left. You need to rewrite these three lines following this model:
Rich (BB code):
d1 = 3*x + a;
Keep in mind that the assignment operator (=) is not the same as the equals sign as used in mathematics. The assignment operator evaluates the expression on the right side of the assignment statement, and stores it in the variable on the left side. If what you have on the left side is not a variable or something that is an lvalue, you'll get the error that you showed.
The condition in your while loop needs to be rewritten, as it probably doesn't mean what you think it does. If you want to check that three variables aren't equal, you need something like this:
Rich (BB code):
while ( (d1 != d2) && (d1 != d3) && (d2 != d3))
{
...
}
Also, with regard to the while loop, you have a double variable being incremented in the body of this loop. For one thing, if you want to increment a variable, it should be of type int or long, but not a real type. More seriously, the loop as written is an infinite loop. Each pass through the loop, your intent is to check d1, d2, and d3 to see if they are unequal. If so, x is incremented. Since a change in x has no effect on d1, d2, or d3, the loop runs again and again, ad infinitum.

Your output line also needs to be rewritten. It should look something like this:
Rich (BB code):
cout << "Your number is " <<  d1 << "." << endl;
I don't think the cout statement you have will work correctly.

Finally, I think you have a problem with the line with the system call in it. What it does is issue a call to the operating system to do something. To the best of my recollection, there is no PAUSE command.

Mark