Thread: invalid lvalue in assignment ?

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    31

    invalid lvalue in assignment ?

    I keep getting that error on this line. "speed - zone = amount;" What did I do wrong? thanks


    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main() {
    
    string input = "";
    
    
    
    int speed;
    double amount;
    //double Samount = 0;
    double ticket = 0;
    int zone;
    
    cout << "Please Enter Zone Speed (20 for school zone):  ";
    cin >> zone;
    
    cout << "Please Enter Speed: ";
    cin >> speed;
    
    
    if (speed > zone) 
    speed - zone = amount;
    amount * 3.00 = ticket;
    print ticket;
    else if (zone = 20) 
    speed - zone = amount
    amount* 6.00 = ticket;
    print ticket;
    
    //if (speed > schoolZone) then
    //speed > schoolZone = Samount;
    //Samount* 6.00 = ticket;
    //print ticket;
    //else
    //print "safe driving"
    
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    How do you expect to be able to assign a value to "speed-zone"? Remember, = is not symmetric: the right side gets stored into the left side.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    Thanks. I'm still learning for forgive me if my questions are basic. On the line I use the "else if" I get this warning. "warning: suggest parentheses around assignment used as truth value". I already have a parentheses there. Why do I get this warning?

    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main() {
    
    string input = "";
    
    
    
    int speed;
    double amount;
    //double Samount = 0;
    double ticket = 0;
    int zone;
    
    cout << "Please Enter Zone Speed (20 for school zone):  ";
    cin >> zone;
    
    cout << "Please Enter Speed: ";
    cin >> speed;
    
    
    if (speed > zone) { 
    amount = speed - zone;
    ticket = amount * 3.00;
    cout <<"Ticket: " << ticket << endl; }
    
    else if  (zone = 20) {
    amount = speed - zone;
    ticket = amount * 6.00;
    cout <<"Ticket: " << ticket << endl; }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What does
    Code:
    zone = 20
    do? Hint: We were just here.

    Now, that will still happen, even if you wrap it up inside an if-statement. Do you want to change the value of zone? Your compiler is doubtful.

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    31
    I'm using an online compiler so that could be an issue. That line checks if the value of 20 is assigned to the variable "zone". If the user inputs 20 for zone then that if statement should run.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MSkiLLz View Post
    That line checks if the value of 20 is assigned to the variable "zone".
    One of the first things programmers learn is the difference between "what I wanted to write" and "what I actually did write". You wanted to write a line that checks if the value of 20 is assigned to the variable "zone". What you actually wrote is a line that puts 20 into the variable "zone" regardless of what was already there. In order to compare, you need to use "==", not "=".

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. invalid lvalue in assignment
    By ChoCo in forum C Programming
    Replies: 2
    Last Post: 08-07-2009, 01:06 PM
  2. :523: error: invalid lvalue in assignment
    By nasim751 in forum C Programming
    Replies: 10
    Last Post: 04-14-2008, 04:08 AM
  3. invalid lvalue in assignment
    By saipkjai in forum C++ Programming
    Replies: 8
    Last Post: 06-21-2007, 03:19 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM