All About Circuits Forum  

Go Back   All About Circuits Forum > Software, Microcomputing, and Communications Forums > Programmer's Corner

Notices

Programmer's Corner Discussion forum for all aspects of programming and software engineering. Any software programming language welcome: C, C++, C#, Fortran, Java, Matlab, etc.

Reply   Post New Thread
 
Thread Tools Display Modes
  #1  
Old 09-13-2006, 02:08 AM
EEMajor EEMajor is offline
Member
 
Join Date: Aug 2006
Location: USA
Posts: 67
Default C++ error: "Non-lvalue error in assignment"

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 newhours = added - (added / 4);

float pretime = added1 + newhours;

static_cast<int>(pt) = 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>(pt) = added1 + newhours;
static_cast<int>(newtime) = newHours + newMinutes;

Again, thank you for the help!
Reply With Quote
  #2  
Old 09-13-2006, 09:17 PM
EEMajor EEMajor is offline
Member
 
Join Date: Aug 2006
Location: USA
Posts: 67
Default The correct way to static_cast!

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:

static_cast<int>(pt) = added1 + newhours;

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:

static_cast<int>(pt = added1 + newhours);

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!

Last edited by EEMajor; 09-13-2006 at 09:29 PM.
Reply With Quote
  #3  
Old 09-14-2006, 10:35 AM
n9352527 n9352527 is offline
Senior Member
 
Join Date: Oct 2005
Posts: 1,198
Default

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.
Reply With Quote
  #4  
Old 09-22-2006, 02:24 PM
EEMajor EEMajor is offline
Member
 
Join Date: Aug 2006
Location: USA
Posts: 67
Default

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

Thanks again.
Reply With Quote
  #5  
Old 07-21-2008, 12:46 PM
alphaboy95 alphaboy95 is offline
New Member
 
Join Date: Jul 2008
Posts: 1
Default Same prob

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;
}



Please help!!!!
Reply With Quote
  #6  
Old 07-21-2008, 02:00 PM
Mark44 Mark44 is offline
Senior Member
 
Join Date: Nov 2007
Location: Snohomish County, USA
Posts: 626
Blog Entries: 2
Default

Quote:
Originally Posted by EEMajor View Post
These are the two static casts I am having problems with:

static_cast<int>(pt) = added1 + newhours;
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.
__________________
"The floggings will continue until morale improves!"

Last edited by Mark44; 07-21-2008 at 02:01 PM. Reason: Removed unneeded part of quote
Reply With Quote
  #7  
Old 07-21-2008, 02:23 PM
Mark44 Mark44 is offline
Senior Member
 
Join Date: Nov 2007
Location: Snohomish County, USA
Posts: 626
Blog Entries: 2
Default

Quote:
Originally Posted by alphaboy95 View Post
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;
}



Please help!!!!
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:
Code:
d1 = 3*x + a;
Select All
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:
Code:
while ( (d1 != d2) && (d1 != d3) && (d2 != d3))
{
    ...
}
Select All
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:
Code:
cout << "Your number is " <<  d1 << "." << endl;
Select All
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
__________________
"The floggings will continue until morale improves!"
Reply With Quote
Reply   Post New Thread

Tags


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:15 AM.


User-posted content, unless source quoted, is licensed under a Creative Commons Public Domain License.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.