error C2106: (populating strings)

This is a discussion on error C2106: (populating strings) within the C++ Programming forums, part of the General Programming Boards category; I'm having trouble populating char arrays. If I populate them when I declare them, I don't get any errors. ie: ...

  1. #1
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    error C2106: (populating strings)

    I'm having trouble populating char arrays. If I populate them when I declare them, I don't get any errors. ie:

    char buffer[3]="ab";

    However if I declare them, then try to populate them later, I get:

    error C2106: '=' : left operand must be l-value (using VC++ 6)

    example code:

    char buffer[3];
    buffer="ab";

    I've also tried:

    buffer<<"ab";

    I can't find any examples in the MSDN library that help. All the ones I found show char being populated when they're declared.
    Any help you can give is appreciated thanks.

    Jason

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    char array[80];
    char *p = "Some string";
    strcpy ( array, p );
    // array now has the contents of p
    cout<<array;
    cin.getline ( array, 80 );
    // array now has whatever the user typed
    cout<<array;
    -Prelude
    My best code is written with the delete key.

  3. #3
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    thanks but...

    Prelude, thanks for your reply, but it seems like I still have to populate a string when it's declared. Even though your example populates a string after it is declared, it populates it with one that was populated when it was declared.
    Also, cout, etc. are all used in DOS programming, correct? I should have mentioned it earlier, but I programming strictly for Windows.

    What I eventually am trying to work towards is something like this:

    In my game I'll have a class, CTrooper, for example, that represents the individual soldiers in the player's army. And I have a string table that lists possible first and last names. I would get a random number that would match one of the entries in each table. That entry would be the soldier's first/last name. ie:

    Code:
    ----PSEUDO---
    //start in the middle of the CTrooper class definition//
    private:
     char FirstName[16];
     char LastName[16];
    
    //then in the function that would get the names randomly
     CTrooper Trooper1;
     Trooper1.FirstName = randomFname;
     Trooper1.LastName = randomLname;
    
    //of course, the CTrooper.FirstName, etc. part isn't working.
    I'm sorry if I seem like I'm lost here. I'm an old VB developer who's trying to make the transition to VC++. So, I really lost on a lot of the proper syntax when it comes to strings, file i/o, and the like.

    Thanks again,
    Jason

  4. #4
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    test

    test

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,788
    The reason what you tried (in the first post) doesn't work is this:

    The buffer variable name gets treated as an address that the compiler/linker assigns when it creates the EXE say for example 0x043BCFE0 or whatever. The "ab" string gets stored in another address because it is a string-literal, let's say for example that it gets the address 0x44C9A002. You cannot then in your code say 0x043BCFE0 = 0x44C9A002 which is essentially what the buffer="ab" code fragment gets converted into. It just doesn't work that way. That is why you get the "left operand must be an l-value" message. You must use something similar to the strcpy() function to input data into the character array.

    Now, if you use a string object, you can assign data to it like you mention. Like so:

    Code:
    #include <string>
    using namespace std;
    int main()
    {
        string buffer;
        buffer = "ab";  // Now buffer equals "ab"
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065

    THANKS!!

    Thanks, hk! That solves my problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 02:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 07:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM

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