lvalue required as left operand of assignment

This is a discussion on lvalue required as left operand of assignment within the C++ Programming forums, part of the General Programming Boards category; i get this error message for each of these lines when i try to run the program. i have no ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    58

    lvalue required as left operand of assignment

    i get this error message for each of these lines when i try to run the program. i have no idea what it means. what am i doing wrong? there is a data file elsewhere in the program, and i'm reading info from that, and one column is numbers from 0-6. each of those corresponds to a name, which is what this function is supposed to do, equate the number to the family name. here's the portion of the code that's getting error
    Code:
    string familyName( int familyCode )
    {
    int n;
    inFile>>n;
    switch (n)
    {
    case 0:
    	0=Drake;
    	break;
    case 1:
    	1=Spencer;
    	break;
    case 2:
    	2=Morgan;
    	break;
    case 3:
    	3=Corinthos;
    	break;
    case 4:
    	4=Webber;
    	break;
    case 5:
    	5=Quatermaine;
    	break;
    case 6:
    	6=Davis;
    	break;
    }
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    = is not symmetric in C++. The value on the right is placed in the variable on the left. Since 0, 1, ..., 6 are not variables, but constants, the assignment cannot happen.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    so i'd have to make each of the numbers variables in order for it to work? but that's not possible, is it?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The problem here is your belief, which I'm not sure where it came from, that somehow you want the number 0 to be assigned the value Drake. That's not what your English text at the top of your original post says you want to do, and it's not what anybody thinking about what your problem would want to do.

    (Hint: Your English text says that, for example, if familyCode is 0 you want some string to be "Drake".
    Last edited by tabstop; 03-26-2010 at 05:01 PM.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2,027
    "equate" does not mean assign. It means create a logical mapping.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  6. #6
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    bear with me here, i'm not knowledgeable at all in c++. i appreciate the help so far. so would declaring the family names as int solve the problem?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by joeman View Post
    bear with me here, i'm not knowledgeable at all in c++. i appreciate the help so far. so would declaring the family names as int solve the problem?
    Is "Drake" a number?

  8. #8
    Registered User
    Join Date
    Feb 2010
    Posts
    58
    ok, how do i set a number equal to the name? i need this program to read 0 from the text file and display drake. i have no idea whatsoever how to do this, i've declared them as strings and ints, i don't know what to do.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by joeman View Post
    ok, how do i set a number equal to the name? i need this program to read 0 from the text file and display drake. i have no idea whatsoever how to do this, i've declared them as strings and ints, i don't know what to do.
    Stop to think for twenty seconds instead of just trying to type the magic word.

    BIG HUGE HINT: Your number and your word are not now, and never have been, supposed to be the same variable.

  10. #10
    Registered User
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    4
    When you use the = operator, it takes the value on the right, and assigns it to a variable on the left. A value can be a number, a letter, a string of text, or another variable among other things.

    This statement:

    Code:
    	0=Drake;
    Won't work because '0' is not a variable (which is causing the "lvalue required as left operand of assignment" error). Another thing to remember is that a string must be put in "quotes", so unless Drake is a variable, that would also be a problem.

    Now, if I understand what you're trying to do, this should fix your problem:

    Code:
    string familyName( int familyCode )
    {
    int n;
    inFile>>n;
    switch (n)
    {
    case 0:
    	cout<<"Drake";
    	break;
    case 1:
    	cout<<"Spencer";
    	break;
    case 2:
    	cout<<"Morgan";
    	break;
    case 3:
    	cout<<"Corinthos";
    	break;
    case 4:
    	cout<<"Webber";
    	break;
    case 5:
    	cout<<"Quatermaine";
    	break;
    case 6:
    	cout<<"Davis";
    	break;
    }
    }
    I'm new to coding myself, so lemme know if I got something wrong, but this should help.

  11. #11
    Registered User
    Join Date
    Feb 2010
    Posts
    58

    Red face

    oh christ, cout<<... why on earth did i not think of that. thank you much, kind sir. thanks to everyone else as well. it's been a long day , ha

  12. #12
    Registered User
    Join Date
    Mar 2010
    Location
    Texas
    Posts
    4

    Smile

    I'm glad I could help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 09:33 AM
  2. Binary Tree Revisited: Near Completion
    By Nakeerb in forum C++ Programming
    Replies: 13
    Last Post: 01-22-2003, 07:23 AM
  3. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 01:24 AM
  4. Release Configuration Problems
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2002, 04:54 AM
  5. Please help me
    By teedee46 in forum C++ Programming
    Replies: 9
    Last Post: 05-06-2002, 11:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21